2012-10-18 43 views
1

它是如此奇怪:爲什麼NSArray的objectAtIndex:對的tableView titleForHeaderInSection錯誤部分:指數0超越界限的空數組

_tableDataSectionTitles是這樣的UITableViewController子類&一個NSArray的屬性。

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
NSLog(@"%@", _tableDataSectionTitles); // returns: ("string") 
NSLog(@"%@", [_tableDataSectionTitles objectAtIndex:0]); // returns: "string" 
NSLog(@"%i", section); // returns 0 
NSLog(@"%@", [_tableDataSectionTitles objectAtIndex:section]; // returns "string" 
return [_tableDataSectionTitles objectAtIndex:section]; // returns: *** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array 

那是什麼!? (其他所有的東西都正常工作,我檢查了三次)(認真的說,這就像是第一件事情,所以沒有什麼應該是錯的)

我也試過了, array:

NSString *rep = [[NSString alloc] init]; 
if (section == 0) { 
    rep = @"Stuff"; 
} else { // doesn't even need it - it won't run through it anyway 
    //rep = [_tableDataSectionTitles objectAtIndex:section]; // commented out just to not have any doubts 
} 
return rep; 

但仍然是相同的錯誤。幫幫我!

+0

if(section == 0){return [_tableDataSectionTitles objectAtIndex:section]; } else {return「你想要的任何東西」} –

+0

粘貼從調試控制檯出現的那些NSLogs的實際輸出。不要試圖將其重新輸入爲評論。 –

回答

0

嗯,我明白了。

因此,事實證明,我的_tableDataSectionContents數組是空的,而_tableDataSectionTitles有一個。所以,我仍然不明白爲什麼它提出了一個完全獨立的數組的錯誤,但它現在起作用!感謝你的幫助。

0

下面的代碼將幫助您檢測你的代碼的錯誤部分伊斯利

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    NSString *title = @"Empty"; 

    NSLog(@"count: %d", [_tableDataSectionTitles count]); 
    NSLog(@"section: %d", section); 

    if([_tableDataSectionTitles count] > section) 
     title = [_tableDataSectionTitles objectAtIndex:section] 

    return title; 

} 
+0

日誌輸出:( - 計數:1 - 部分:0 - *** - [__ NSArrayI objectAtIndex:]:索引0超出空數組的邊界 - )所以它仍然是同樣的事情。錯誤在標題= [_tableDataSectionTitles objectAtIndex:節] – Conflagrationator

+0

請檢查tableDataSectionTitles範圍及其內容 –

0

顯然,錯誤說[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array 意義,數組是空的,即使它不應該。所以,我不知道到底是什麼問題,但你可以按照如下步驟來調試問題,

  1. 檢查它是否只發生爲第0不是傳遞部分objectAtIndex的,通過0和嘗試。
  2. 返回僅當數組不爲空的標題,

    如果([_ tableDataSectionTitles計數]> 0){ 返回 [_tableDataSectionTitles objectAtIndex:節]。 } else { return @「Default Title」; }

  3. 有alloc在你的代碼中的某處初始化數組.. ..?可能在ViewDidLoad或init方法中。如果不這樣做,並嘗試。

如果您再次遇到任何奇怪的事情,請告訴我們。希望它有助於.. :)

+0

好吧,我已經嘗試把它與數字0,也與if statemet。但是,(這真的很奇怪),當我把方法放在一個NSLog與任一或兩個方法(0和部分)它返回罰款,但當我調用任何方法外(即使在if語句)它結束了與同樣的錯誤。 - 更不用說NSLog只是方法調用的一行。 – Conflagrationator

相關問題