iOS-deleteRowsAtIndexPaths 崩溃

标签 ios objective-c uitableview ios6

我正在尝试使用以下代码删除。

[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

它返回多个异常。

 *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1070
2013-01-29 16:28:22.628 

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 5 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

最佳答案

这是因为您应该有一种返回行数的动态方式。

例如,我创建了 3 个数组。每个都有 3 个值(这些是 NSArray 变量):

.h文件中:

NSArray *firstArray;
NSArray *secondArray;
NSArray *thirdArray;

.m 文件中,viewDidLoad 或 init 或类似的内容:

firstArray = [NSArray arrayWithObjects:@"Cat", @"Mouse", @"Dog", nil];
secondArray = [NSArray arrayWithObjects:@"Plane", @"Car", @"Truck", nil];
thirdArray = [NSArray arrayWithObjects:@"Bread", @"Peanuts", @"Ham", nil];

当返回表中的行数时,我有:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         return array.count;
      if (section == 0) {
          return firstArray.count;
      } else if (section == 1) {
          return secondArray.count;
      } else {
          return thirdArray.count;
      }
}

然后,在cellForRow中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    if (indexPath.section == 0) {
    cell.textLabel.text = [firstArray objectAtIndex:indexPath.row];
    } else if (indexPath.section == 1) {
        cell.textLabel.text = [secondArray objectAtIndex:indexPath.row];
    } else {
        cell.textLabel.text = [thirdArray objectAtIndex:indexPath.row];
    }

    return cell;
}

然后我通过在 table 上滑动或您想要删除的其他方式来删除@“Dog”。然后,当重新加载表格时,数组计数将为 2,因此表格将“知道”它必须仅显示 2 行。基本上,您还需要更新数据源。 它也适用于其他部分。因为您从数组中删除元素,所以行数也会更新。

关于iOS-deleteRowsAtIndexPaths 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14581923/

相关文章:

ios - 在请求中添加用户信息

ios - Objective-C:在不损失精度的情况下将 float 转换为 int64_t

objective-c - NSBezierPath 对象似乎正在绘制可变线宽

uitableview - allowsMultipleSelection 在 Swift 中不起作用?

ios - 使用 Storyboard创建可重用的 UITableView

ios - 在iOS 7中的cellForRowAtIndexPath之后没有调用heightForRowAtIndexPath

ios - (Swift) UICollectionViewCell 带有手势识别器的嵌套 subview

ios - 单击每个页面的选项卡项转到主页选项卡

ios - 在iOS上运行Flutter项目时出现重新定义错误

ios - react native : Unwanted Black border on ipad 2 simulation