inVINCEable
inVINCEable

Reputation: 2206

Delete Multiple Rows from UITableView

I currently have 2 methods to delete rows from a UITableView, the first one is the standard slide then press delete, the second deletes all rows that are checked. The only problem is when you select more then 1 or 2 it crashes with the following in the NSLog

2014-04-08 22:37:53.985 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000038016> {length = 2, path 

= 0 - 7}
2014-04-08 22:37:53.987 PunchSlip[13128:60b] 7
2014-04-08 22:37:53.990 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000030016> {length = 2, path = 0 - 6}
2014-04-08 22:37:53.991 PunchSlip[13128:60b] 6
2014-04-08 22:37:53.993 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000028016> {length = 2, path = 0 - 5}
2014-04-08 22:37:53.995 PunchSlip[13128:60b] 5
2014-04-08 22:37:53.997 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000018016> {length = 2, path = 0 - 3}
2014-04-08 22:37:53.999 PunchSlip[13128:60b] 3
2014-04-08 22:37:54.001 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000020016> {length = 2, path = 0 - 4}
2014-04-08 22:37:54.003 PunchSlip[13128:60b] 4
2014-04-08 22:37:54.005 PunchSlip[13128:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray removeObjectAtIndex:]: index (4) beyond bounds (4)'

My first delete method looks like this:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete) {

        //delete row from array and from plist

        [arrayFromPlist removeObjectAtIndex:indexPath.row];


        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"break.plist"];
        [arrayFromPlist writeToFile:plistLocation atomically:YES];

        [self.tableeView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        //[self.tableeView reloadData];

    }
}

My Second Method, which i based off off the first looks like this :

-(IBAction)erase:(id)sender{
    NSArray *selectedCells = [self.tableeView indexPathsForSelectedRows];
    for (NSString *string in selectedCells) {
        NSLog(@"%@", string);
        NSString *stringOne = [NSString stringWithFormat:@"%@", string];

        NSString *thisOne = [[stringOne componentsSeparatedByString:@" "] objectAtIndex:9];
        NSString *thatOne = [[thisOne componentsSeparatedByString:@"}"] objectAtIndex:0];
        int rowInt = [thatOne intValue];
        NSLog(@"%d", rowInt);


        [arrayFromPlist removeObjectAtIndex:rowInt];


       // [tableeView deleteRowsAtIndexPaths:[NSArray arrayWithObject:string] withRowAnimation:UITableViewRowAnimationAutomatic];

        [tableeView reloadData];


    }


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"break.plist"];
    [arrayFromPlist writeToFile:plistLocation atomically:YES];
    [self viewDidLoad];


}

The second method knows which cells are selected as you can see in the log, but when you do "too many" in this case 4 it says there is an NSRangeExeption.

Any Ideas on how to correct it?

Upvotes: 1

Views: 2923

Answers (1)

Akhilrajtr
Akhilrajtr

Reputation: 5182

Try this for second method,

-(IBAction)erase:(id)sender{
    NSArray *selectedCells = [self.tableeView indexPathsForSelectedRows];
    NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
    for (NSIndexPath *indexPath in selectedCells) {
        [indicesToDelete addIndex:indexPath.row];
    }
    //arrayFromPlist is NSMutableArray
    [arrayFromPlist removeObjectsAtIndexes:indicesToDelete];
    [tableeView beginUpdates];
    [tableeView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableeView endUpdates];

    ....
    ....
}

Upvotes: 6

Related Questions