2010-06-01 93 views
0

我想根据用户的选择从表视图中删除多行。显然,我不能使用didSelectRowAtIndexPath方法,因为它会被选中的每一行都被调用。我想允许用户选择多行进行删除,然后一次删除它们。可能吗?UITableView:我可以删除多行吗?

如果是,那么该怎么办呢?另外,我正在使用基于单个视图的项目,并且希望当用户想要从视图中删除行时,在同一视图中将表视图的标题更改为“删除”。

+0

PLZ接受一些回应。谢谢 – kikito 2010-06-01 18:25:11

回答

1

你可以做一些事情是这样的:

- (void)tableView:(UITableView *)theTableView 
     didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath { 

[theTableView deselectRowAtIndexPath:[theTableView indexPathForSelectedRow] animated:NO]; 
UITableViewCell *cell = [theTableView cellForRowAtIndexPath:newIndexPath]; 
if (cell.accessoryType == UITableViewCellAccessoryNone) { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [selectedCellsMutableArray addObject:newIndexPath]; 
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    [selectedCellsMutableArray removeObjectIdenticalTo:newIndexPath]; 
} 

}

当用户按下删除选定按钮 - 只需调用像

// change your model here and then: 
[yourView deleteRowsAtIndexPaths:selectedCellsMutableArray 
       withRowAnimation:UITableViewRowAnimationRight]; 
0

逾期答复,而是另辟蹊径要处理这个问题(如果你的目标iOS 5)是切换编辑模式,并使用内置的多个单元格选择。要做到这一点最简单的方法就是把这个在viewDidLoad中......

// add edit button to navigation bar, which auto toggles editing mode 
self.navigationItem.rightBarButtonItem = self.editButtonItem; 
// allows user to select multiple cells while in editing mode 
self.tableView.allowsMultipleSelectionDuringEditing = YES; 

See documentation for more details