繁体   English   中英

如何删除表视图中的单元格

[英]how to delete the cell in table view

有人可以建议我的代码中存在什么问题.....

self.modleClass.foldersAray有一个对象,tableview有一个section和一行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source
        [self.tableview setEditing:YES animated:YES];

        [self.modleClass.foldersAray removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YEs];

        [tableView reloadData];
   }
}

我收到的错误如下。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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).'

这样做,

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

      if (editingStyle == UITableViewCellEditingStyleDelete) {

          [self.modleClass.foldersAray removeObjectAtIndex:indexPath.row];

          [tableView reloadData];
      }
}

它够了.........

如果tableView基于foldersAray ,则从该数组中删除对象并重新加载tableview将导致删除此单元格。 您可以删除该行:

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YEs];

此错误意味着您必须更新数据源(包含数据,字典或您使用的任何内容的数组)

例如:删除前有5个单元格,dataArray中有5个对象。 在调用deleteRowsAtIndexPaths:之前deleteRowsAtIndexPaths:我们必须从dataArray中删除与该单元格关联的对象,然后才调用deleteRowsAtIndexPaths: . 而是使用[tableView reloadData] ,使用

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

PS我看,你从数据源中删除对象,所以仔细检查,也许你错过了什么

如果你想要动画试试这个,如果你不想动画使用@Mountain Lion的答案。

 -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
 forRowAtIndexPath:(NSIndexPath *)indexPath  {

       if (editingStyle == UITableViewCellEditingStyleDelete) 
         {
             [self.modleClass.foldersAray removeObjectAtIndex:indexPath.row];
             [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];
       } 
 }

如果你在导航上有编辑按钮,你应该打电话

[self.tableView setEditing:YES animated:YES];

在你的方法里面开始编辑。

如果你需要的内存对你来说无关紧要......那么你可以通过将高度设置为0来显着删除(隐藏)单元格。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row%2) {
        return 0;
    }
    return 100;
}

PS:上面的虚拟代码将大大删除备用单元格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM