删除cell时,出现崩溃的情况的原因

来源:互联网 发布:游戏台式机推荐 知乎 编辑:程序博客网 时间:2024/05/14 06:00

一、删除cell时,出现崩溃的情况的原因:

在 UITableView 中,做删除操作时,我们会使用一下这个方法:

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

其中,[NSArray arrayWithObject:indexPath] 是我们要删除的 cell 的 indexPath 值。由于我们可能会删除多个 cell,所以 indexPath 会放在一个数组里。

当我们使用这个方法对 cell 做删除操作后,UITableView 会调用 reloadData 的方法,对 tableView 做刷新处理。这个时候,如果我们在

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

这个代理方法中,把 cell 的 row 值,写成了某一固定的具体数字,
而这时,我们删除的 dataSource 又恰恰是 最后一个时,就会出现崩溃。

二、总结:

在对 tableview 做删除操作时:

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

此方法删除cell后的行数要和

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

的返回值,对应上。
也就是说 numberOfRowsInSection 方法的返回值,最好不要用 固定值。

1 0
原创粉丝点击