开发者

Accesing objects in multidimensional array, Obj-c

I have an array holding other arrays, but can开发者_开发知识库t get this to work!! IM trying to pull string values from subArrays I can access first the first subarray no problem but then when i try changing the label to the object in second array my prog crashes anyidea on how i should approach this?

int count = 0; // variable to access the required sub array 
NSArray* myArray; // array holding other arrays 
UILabel* mylabel; // label to display my string values from the array s

-(void) setLabel 
{

    NSArray* subArray = [myArray objectAtIndex: count];
    [myLabel setText:[subArray objectAtIndex:1]]; // this works fine
}

-(void) changeLabelToNextArray
{
    count ++
    [self setLabel]; //program crashes here when try to load label from next array 
} 


Why are you doing this?

[myLabel setText:[subArray objectAtIndex:1]];

This will crash if

  1. subArray does not have an object at index 1
  2. the object at index 1 cannot be set as the label text (i.e cannot be made a string)

I think some more information on how your arrays are structured would help give a better answer to what the problem is.

EDIT (Based on comments below) Try this:

NSArray* myArray;   // Contains 5 subarrays, each containing 5 strings

UILabel* myLabel1;
UILabel* myLabel2;
UILabel* myLabel3;
UILabel* myLabel4;
UILabel* myLabel5;

int count = 0;      // Keeps track of which subarray we are on

- (void)setLabel
{
    NSArray* subArray = [myArray objectAtIndex:count];
    [myLabel1 setText: [subArray objectAtIndex:0]]
    [myLabel2 setText: [subArray objectAtIndex:1]]
    [myLabel3 setText: [subArray objectAtIndex:2]]
    [myLabel4 setText: [subArray objectAtIndex:3]]
    [myLabel5 setText: [subArray objectAtIndex:4]]
    count = (count + 1) % 5;    // Ensures that count is always 0 to 4
}

Now whenever you call setLabel, the text should change on all 5 of your labels provided you do indeed have 5 strings in each of the 5 subarrays.


Maybe you can try to use C array :

id myArray[iMax][jMax];
subArray[i][j] = myArray[a][b];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜