【问题标题】:UITableView cell deletion crash?UITableView 单元格删除崩溃?
【发布时间】:2015-08-24 23:32:18
【问题描述】:

所以我有这个代码来删除我的 tableview 中的一行:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray removeObjectAtIndex:indexPath.row/2];
    [[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:YES];
}

现在我做 indexPath.row/2 因为我这样做:

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

只是因为它使我的 UI 看起来像现在这样。无论如何,这是崩溃:

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

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
    [self.cellArray removeObjectAtIndex:indexPath.row/2];
    [[NSUserDefaults standardUserDefaults] setObject:self.cellArray forKey:@"cellArray"];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nextPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}

【问题讨论】:

    标签: ios uitableview cells


    【解决方案1】:

    问题是您在元胞数组中为每个对象使用两个表格行。但是在您发布的代码中,您只从表中删除了一行。您需要删除两行。

    NSIndexPath *nextPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
    [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObjects:indexPath, nextPath, nil] withRowAnimation: UITableViewRowAnimationFade];
    

    顺便说一句 - YES 不是行动画的有效参数。

    【讨论】:

    • 检查我发布的更新代码,你说我应该这样做吗?
    • 是的。但不要像那样修改你原来的问题。现在没有人知道出了什么问题。您应该放回代码,然后在问题的末尾添加更新的代码,以便人们可以看到历史记录。
    【解决方案2】:

    也许这只是这一行的一个小错字。

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        [self.cellArray removeObjectAtIndex:indexPath.row]; // Returning a number instead of possible double
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多