从UITableView删除多行

时间:2014-04-09 02:44:40

标签: ios objective-c uitableview ios7

我目前有两种方法可以从UITableView中删除行,第一种是标准幻灯片,然后按删除,第二种删除所有检查的行。唯一的问题是当您选择多于1或2时,它会在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)'

我的第一个删除方法如下:

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

    }
}

我的第二种方法,我基于第一种方法看起来像这样:

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


}

第二种方法知道在日志中可以看到哪些单元格被选中,但是当你做的时候太多了#34;在这种情况下,它说有一个NSRangeExeption。

关于如何纠正它的任何想法?

1 个答案:

答案 0 :(得分:6)

尝试第二种方法,

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

    ....
    ....
}