2014-04-09 128 views
1

我目前有兩種方法可以從UITableView中刪除行,第一種是標準幻燈片,然後按下刪除鍵,第二種刪除所有被選中的行。唯一的問題是,當你選擇更多然後1或2它在NSLog的從UITableView中刪除多行

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


} 

第二種方法知道哪些小區作爲你可以在日誌中看到的,但是當你在這種情況下,4做「太多」它說,有一個NSRangeExeption。

任何想法如何糾正它?

回答

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

    .... 
    .... 
}