2013-02-22 55 views
1

有一个人所说的其实是在我的代码的问题小区.....如何删除表视图

self.modleClass.foldersAray有一个对象,并实现代码如下有一个部分和一排

- (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).' 

回答

0

此错误意味着,你必须更新你的数据源(数组,不管你使用的数据字典或)

例如: 你有5个前删除和5对象DataArray中。在致电deleteRowsAtIndexPaths:之前,我们必须从dataArray中删除与该单元关联的对象,然后才能拨打deleteRowsAtIndexPaths:。也可改为使用[tableView reloadData],使用

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

P.S.我明白了,您从数据源中删除的对象,所以仔细检查它,也许你错过的东西

2

做这样的,

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

     if (editingStyle == UITableViewCellEditingStyleDelete) { 

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

      [tableView reloadData]; 
     } 
} 

更足够.........

+0

我这样做,但细胞没有删除...它显示当我点击编辑按钮时的相同状态... – user1997077 2013-02-22 12:28:24

2

如果您的tableView基于foldersAray,那么从该数组中删除一个对象并重新加载tableview将导致删除该单元格。 你可以删除行:如果你想动画试试这个,如果你不想使用动画狮子@Mountain的回答

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

-(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动画:是];

内部的方法开始编辑。

1

,如果它需要你没关系的记忆...那么你就可以极大地设置高度删除(隐藏)电池为0

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

P.S:上述虚设码将极大地删除备用电池。