deleteRowsAtIndexPaths不起作用[self.tableView reloadData]

时间:2014-05-03 20:58:35

标签: ios objective-c uitableview

我正在使用SWTableViewCell,我有一个删除行的按钮。通常我会使用commitEditingStyle并没有问题,但SWTableViewCell rightUtilityButtons似乎导致应用程序崩溃。这是代码:

[self.tableView beginUpdates];
[context deleteObject:client];
NSError *error = nil;
if(![context save:&error]){
            NSLog(@"OOPS");
        }else{
            [self.clients removeObjectAtIndex:cellIndexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationRight];
        }
[self.tableView endUpdates];

这是错误:

  

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'

我意识到错误是因为我的数据源正在返回一个nil对象。我搞砸了一下,尝试了几件事。如果我删除[self.tableView beginUpdates][self.tableView endUpdates]并添加[self.tableView reloadData][self viewDidAppear:],我都不会这样做,因为单元格只是闪烁不存在。

我的tableView:numberOfRowsInSection:只会触发一次,除非我使用[self.tableView reloadData]

为了让deleteRowsAtIndexPaths:withRowAnimation:工作,我做错了什么。

修改的 获取cellIndexPath:

NSIndexPath *cellIndexPath = [self.tableView indexPathForSelectedRow];

1 个答案:

答案 0 :(得分:1)

只需替换

    if(![context save:&error]){
        NSLog(@"OOPS");
    }else{
        [self.clients removeObjectAtIndex:cellIndexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationRight];
    }

        if([context save:&error] && cellIndexPath){
            [self.clients removeObjectAtIndex:cellIndexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationRight];
        }
相关问题