简体   繁体   中英

deleteRowsAtIndexPaths doesn't work however [self.tableView reloadData] does

I am using SWTableViewCell s and I have a button for deleting the row. usually I would just use the commitEditingStyle and have no problems but with SWTableViewCell rightUtilityButtons it seems to cause the app to crash. Here's the Code:

[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];

This is the error:

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

I realize the error is because my datasource is returning a nil object. I messed with it a bit and tried a few things. It does work if I remove the [self.tableView beginUpdates] and [self.tableView endUpdates] and add either [self.tableView reloadData] or [self viewDidAppear:] , neither of which I like because the cell just blinks out of existence.

My tableView:numberOfRowsInSection: only fires once unless I use [self.tableView reloadData] .

What am I doing wrong in order to get deleteRowsAtIndexPaths:withRowAnimation: working.

EDIT to get cellIndexPath:

NSIndexPath *cellIndexPath = [self.tableView indexPathForSelectedRow];

just replace

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM