滑动即可删除uitableview单元格

时间:2016-08-03 17:34:25

标签: ios objective-c uitableview

强调textreason:

Invalid update: invalid number of rows in section 0.   
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, 1 deleted) 
and plus or minus the number of rows moved into or out of that 
section (0 moved in, 0 moved out).

***首先抛出调用堆栈

我在尝试删除

时遇到此错误

3 个答案:

答案 0 :(得分:1)

此错误意味着在删除后更新UITableView时,您用作数据源的阵列尚未更新。

换句话说:

您在UITableViewDelegate的ggtitle(leg[$l,1])中使用了NSArray dataArray。 您可以为单元格设置动画以删除该行,但之后您永远不会调用

cellForRowAtIndexPath

因此,当表再次呈现时,UITableView期望有5个项目,但您的数组仍然有6个。

答案 1 :(得分:0)

您的问题不明确,请按以下步骤操作

  1. 在委托方法commitEditingStyle中,从数组中删除对象。
  2. 然后在具有更新数组的表上应用deleterow动画。
  3. 我认为你不会从你的集合中删除对象,即数组

答案 2 :(得分:0)

试试这段代码。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
         [tableDataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
       [self.tableView reloadData];
}
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;

}

如需更多参考,请访问here

相关问题