IOS -- 调用deleteRowsAtIndexPaths:withRowAnimation:崩溃的问题

本文是集合了大神们的解答,仅用于自己的记载和查看

运行

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

报错

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),

经过无头绪的查询看到了这些个解决方案
一、

[dataArr removeObjectAtIndex:indexPath.section];//这句是肯定要有的
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];

二、

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

还是没有解决根本问题 最后的最后

调用deleteRowsAtIndexPaths:withRowAnimation:withRowAnimation:
后如果tableView:numberOfRowsInSection返回的行数不是删除前-1则会crash,同样删除最后一行导致numberOfSectionsInTableView返回值跟调用前不一样而crash。解决方法,调用deleteRowsAtIndexPaths:withRowAnimation:时判断是不是该section最后一行,如果时则同时调用deleteSections:indexSetWithIndex:withRowAnimation:

发现原来我删的是section 换一下方法就好了

你可能感兴趣的:(学习笔记)