【问题标题】:TableView app terminated due to 'NSInternalInconsistencyException'TableView 应用程序因“NSInternalInconsistencyException”而终止
【发布时间】:2011-09-04 19:25:53
【问题描述】:

我正在尝试掌握 UITableViews 以及与之相关的所有内容。目前我有以下代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return 10; 
} 

-(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];
     }

     [cell.textLabel setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];

     return cell; 
}

- (IBAction)killItem {
     NSIndexPath *indexToDelete = [NSIndexPath indexPathForRow:2 inSection:0];
     [tbl deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexToDelete] withRowAnimation:UITableViewRowAnimationRight];
}

我在启动“killItem”功能时收到以下错误:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:第0节中的行数无效。更新后现有节中包含的行数(10)必须等于行数更新前包含在该节中 (10),加上或减去从该节插入或删除的行数(0 插入,1 删除)。'

我理解它的方式 tableViews 基本上有一个委托和一个数据源,其中数据源决定了 tableView 中应该有多少行。通过在 stackoverflow 上的一些搜索,我发现当“数据源与现实不匹配”时,当它正在搜索我已删除的不存在的行时,会导致此错误。

我可能弄错了,但这就是我认为的做法。所以我的问题是,我如何让这些匹配,这样我就可以避免这个错误?

作为参考,我在不了解我需要做什么的情况下查看了以下帖子:

Error : Number of Rows In Section in UITableView in iPhone SDK

Slide UITableViewCell

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

在 killItem 函数中添加[tbl reloadData][tbl beginUpdate] ... [tbl endUpdate] 似乎并没有解决我的问题。

提前谢谢你, 托拜厄斯·托维达尔

【问题讨论】:

    标签: iphone objective-c ios uitableview


    【解决方案1】:

    Tobias,删除行时需要做的是

    // tell the table view you're going to make an update
    [tableView beginUpdates];
    
    // update the data object that is supplying data for this table
    // ( the object used by tableView:numberOfRowsInSection: )
    [dataArray removeObjectAtIndex:indexPath.row];
    
    // tell the table view to delete the row
    [tableView deleteRowsAtIndexPaths:indexPath 
               withRowAnimation:UITableViewRowAnimationRight];
    
    // tell the table view that you're done
    [tableView endUpdates];
    


    当您调用endUpdate 时,从tableView:numberOfRowsInSection: 返回的数字必须与beginUpdate 的数字减去删除的行数相同。

    【讨论】:

      【解决方案2】:

      其实很简单,问题就在这里:

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
          return 10; 
      }
      

      Apple 使用的委托模式,意味着您是负责通过其委托管理 UITableView 内容的人,这意味着,如果您删除一行,您还负责从数据中删除数据型号。

      因此,在删除一行之后,section 中的行数会减少到“9”是有道理的,但是您的函数总是返回 10,从而引发异常。

      通常,当使用表格时,内容会发生变化,NSMutableArray 很常见,你可以这样做:

      - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {
          return [arrayWithStuff count];
      }
      

      然后,从数组中删除一个对象 (removeObjectAtIndex:) 会自动更新行数。

      (编辑:与 Mike Hay 几乎同时回复,也尝试听从他的建议!我跳过了开始/结束更新,因为您似乎已经阅读过)

      【讨论】:

        猜你喜欢
        • 2020-01-26
        • 1970-01-01
        • 2015-11-10
        • 2014-01-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-28
        相关资源
        最近更新 更多