美文网首页iOS Developer
iOS11Crash: UITableView Invalid

iOS11Crash: UITableView Invalid

作者: 呆萌的中指 | 来源:发表于2017-06-15 18:01 被阅读0次

在iOS11的系统上,当删除一个cell,重新添加新的cell,再刷新UITableView时,必定会出现Crash。

提示大概是这样的:


crash.png

它主要的意思就是你明明删除了一个,可是刷新的时候返回cell的数量还是原来的,所以crash,难道苹果用了神奇的AI,并不是苹果的锅?至于为什么iOS11会这样,我并没有找到结果,希望有好心人告诉我。

核心代码:

//删除了一个又添加了一个数据源
- (void)resetdataSourceWithIndex:(NSInteger)index {
    NSMutableArray *mTempArr = [NSMutableArray arrayWithArray:self.dataSource];
    [mTempArr removeObjectAtIndex:index];
    [mTempArr addObject:[NSString stringWithFormat:@"test%@", @(self.num + 1)]];
    self.num ++;
    self.dataSource = mTempArr;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource.count - 1 < indexPath.row)
    {
        return;
    }
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //先删数据源 
        [self resetdataSourceWithIndex:indexPath.row];
        //再刷新tableView
        [self.tableView reloadData];
    }
}

删除cell之后,在数据源个数不变的情况下,直接调用[self.tableView reloadData]来刷新tableView,在iOS11上直接crash,有兴趣的同学也可以亲自试一下。

我目前的解决方案是换一种刷新的方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource.count - 1 < indexPath.row)
    {
        return;
    }
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self resetdataSourceWithIndex:indexPath.row];
        [self.tableView beginUpdates];
        [self.tableView reloadSections:[[NSIndexSet alloc]initWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.tableView endUpdates];
    }
}

如果你有什么好的办法或者我的理解有什么问题,欢迎留言。

相关文章

网友评论

    本文标题:iOS11Crash: UITableView Invalid

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