2011-04-21 64 views
0

我將數據插入到表格視圖中,如下所示。還有如果有超過6個然後應用程序崩潰的5至6元沒問題,並顯示錯誤:爲什麼我在將第六個元素插入tableview的時候出現NSRangeException?

"Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 11 beyond bounds [0 .. 10]' "

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    return [ProductArray count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

     NSDictionary *aDict = [ProductArray objectAtIndex:indexPath.row];  
     cell.textLabel.text= [aDict objectForKey:@"ProductName"]; 


    return cell; 
} 
+1

你能後與您插入新的數據的代碼? – 2011-04-21 09:13:33

+0

它墜毀超過6層的元件,因爲這是當表是可滾動的。 UITableView在滾動時加載行。 – samwize 2011-04-21 10:01:55

回答

0

顯然墜機消息表明您試圖訪問其變爲一個元素超過數組可以包含的元素的限制。請檢查您是否更新您的tableview和數組中的numberOfRows。

0

numberOfRowsInSection必須設置正確數量的行oldNumberOfRows + addedRowsNumber量。

編輯:

而且在ProductArray添加dictionaryes每一行添加行的實現代碼如下之前。

1

使用此代碼

if(indexPath.row < [ProductArray count]){ 

NSDictionary *aDict = [ProductArray objectAtIndex:indexPath.row]; 
cell.textLabel.text= [aDict objectForKey:@"ProductName"]; 

} 
0

的錯誤提示,你想在你的NSMutableArray 11(第12屆對象)的指數或更高版本,僅擁有11名對象訪問的對象。數組索引從0開始,這可能是問題所在。試着對ProductArray的大小做一個NSLog,並檢查它保存的對象數量。正如很多人在崩潰之前所說的那樣,可能是RowInSection方法的數量。如果你不想與ProductArray大小打擾,只返回一個固定數量,看看代碼的其餘部分都很好。

相關問題