UITableView 删除ROW 程序崩溃

UITableView 删除ROW 调用方法:[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

但是会引起.

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

解决方法.

1.删除DATABASE里的元素,并保证DATABASE的个数与删除后的元素个数相同!

2.[tableView beginUpdates];

3.[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];

4.[tableView endUpdates];


如下:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
int row = indexPath.row;
[g_zxgArray removeObjectAtIndex:row];
_needSave = YES;
    [self.stockArray removeObjectAtIndex:row];
    _recvLines -= 1;
    
    [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
// [tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

{

    return _recvLines;

}

说明 :

It's likely that an NSInternalInconsistencyException is being raised (check your console).

You need to ensure that when tableView:numberOfRowsInSection: is called on your UITableViewDelegate, the count is correct for the section of the cell that was deleted via deleteRowsAtIndexPaths:withRowAnimation:. i.e. If there were 10 elements in section 0 and you delete one of them viadeleteRowsAtIndexPaths:withRowAnimation:tableView:numberOfRowsInSection:must return 9 for section 0.


还是应该使用ARRARY返回个数, 不应该使用变量. 两个小时... 代码质量.


原文地址:http://blog.csdn.net/allinone2/article/details/7717668

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值