美文网首页
iOS 8.x reload时tableView:canEdit

iOS 8.x reload时tableView:canEdit

作者: 平凡码农 | 来源:发表于2019-04-29 16:18 被阅读0次

当我们更改了dataSource(删除了section或者row),并调用了[tableview reloadData],但- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section还没来及的调用,却调用了- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath导致数组越界崩溃。

正确的路子是删除dataSource后对tableview的row及section进行删除,而非reloadData。

[model.list removeObjectSafeAtIndex:indexPath.row];
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];    
[self.dataArray removeObjectSafeAtIndex:index];
[self.tableView beginUpdates];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];

相关文章

网友评论

      本文标题:iOS 8.x reload时tableView:canEdit

      本文链接:https://www.haomeiwen.com/subject/qyzynqtx.html