2012-03-19 58 views
1

有没有人可以请解释一下如何正确实施tableView:commitEditingStyle:forRowAtIndexPath:方法,以便我可以正确删除支持数组中的项目并仅显示尚未删除的项目?正确地从UITableView中删除一个项目

这是我到目前为止有:

@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> 
{ 
    IBOutlet UITableView *tableView; 

    NSArray *allItems; 
    NSMutableArray *displayItems; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    allItems = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven", nil]; 
    displayItems = [[NSMutableArray alloc] initWithArray:allItems]; 
} 

-(UITableViewCell*) tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 
    cell.textLabel.text = [displayItems objectAtIndex:indexPath.row]; 
    return cell; 
} 

@end 

回答

3

试试这个:

// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source. 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     if(!deletedIndices) deletedIndices = [[NSMutableIndexSet alloc] initWithIndex:indexPath.row]; 
     else [deletedIndices addIndex:indexPath.row]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 

-(IBAction)editingDidComplete:(id)sender 
{ 

    // remove the objects from display array 
    [valueArray removeObjectsAtIndexes:deletedIndices]; 
    // Reload tableView if needed 
    [mainTableView reloadData]; 
    [mainTableView setEditing:NO]; 
} 
+0

谢谢!非常感激! – nemesis 2012-03-19 13:05:33

+0

崩溃:由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效的更新:在第0节中的行数无效。更新(7)后现有节中包含的行数必须等于在更新前包含在该部分中的行(7),加上或减去从该部分插入或删除的行数(0插入,1删除)以及加或减移入或移出该部分的行数(0移动中,0移出)。' – nemesis 2012-03-19 16:40:46

3

实现你tableView:commitEditingStyle:forRowAtIndexPath:如下:

// Override to support editing the table view. 
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     // remove the object from display array 
     [displayItems removeObjectAtIndex:indexPath.row]; 

     // Delete the row from the data source. 
     [aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 
+0

非常感谢!希望这会起作用! ;-) – nemesis 2012-03-19 13:06:27

+0

崩溃:由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效更新:节0中的行数无效。更新后现有节中包含的行数(7)必须等于在更新之前包含在该部分中的行数(7),加上或减去从该部分插入或删除的行数(0插入,1删除)以及加或减移入或移出该部分的行数(0移入,0移出)。' – nemesis 2012-03-19 16:40:52

+0

你可以定位。让我们知道您使用的是哪个代码? – Ilanchezhian 2012-03-20 03:50:17