iOS 用editActionsForRowAtIndexPath:删除cell时候回出现崩溃

先上代码


// 删除cell操作

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    __weak typeof(self)weakSelf = self;

    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        [weakSelf.dataSource removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }];

    return @[delete];

}


报错如下:


*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.52.10/UITableView.m:2012


哈哈哈哈哈哈,,,忍不住笑自己逗比了,哈哈哈哈


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

    return _dataSource.count; // 刚刚这里调试之前写死的数据,哈哈哈哈

}


再不行,看到网上有人添加了两句


// 删除cell操作

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    __weak typeof(self)weakSelf = self;

    UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {

        [weakSelf.dataSource removeObjectAtIndex:indexPath.row];

        [tableView beginUpdates]; // 开始刷新

        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates]; // 结束刷新

    }];

    return @[delete];

}


但是本人亲测,要不要这两句都一样可以正常运行,等下再看看是否会有其他影响。

猜你喜欢

转载自blog.csdn.net/jwheat/article/details/79965734
今日推荐