UITableView 删除ROW 程序崩溃

来源:互联网 发布:虫虫大作战刷气球软件 编辑:程序博客网 时间:2024/06/16 14:53

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
原创粉丝点击