2011-04-21 41 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大小打扰,只返回一个固定数量,看看代码的其余部分都很好。

相关问题