2011-08-26 32 views
0

我有一個數組持有其他數組,但不能得到這個工作!即時消息試圖從子字符串拉字符串值我可以訪問第一個子數組沒有問題,但後來當我嘗試將標籤更改爲第二個數組中的對象我的程序崩潰anyidea我應該如何處理這個?訪問多維數組中的對象,Obj-c

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 
} 

回答

0

你爲什麼要這麼做?

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

如果

  1. 子陣列並不在索引1
  2. 在索引1處的對象不能被設置爲標籤文本(即不能由字符串)
  3. 有一個對象這會崩潰

我想了解一些有關陣列結構的更多信息將有助於更好地回答問題所在。

EDIT(基於下面的評論) 試試這個:

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 
} 

現在只要您撥打setLabel,案文應在標籤上的所有5提供改變你確實有5串在每個5子陣。

+0

嘿feanor,我有一個數組與5個子數組每個子數組有5個字符串,即時嘗試顯示 – Rhuntr

+0

因此,你總共有25個字符串,你想你的方法一次更改UILabel顯示的文本? – Feanor

+0

有點,我會有5標籤,並希望顯示從第一個子數組的5個字符串,然後更改5標籤顯示從第二個子數組的5個字符串 – Rhuntr

0

也許你可以嘗試用C數組:

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