在UITableView中同时编辑和删除多行

在我的应用程序中,我需要删除表中的多行,编辑表并获取表旁边的checkbox。 当选中时,表格单元格被删除。 这就像iPhone的消息应用程序。 我该怎么做,请帮助我。

如果我正确理解你的问题,你基本上想以某种方式标记 UITableViewCell (一个复选标记)。 然后,当用户点击一个主“删除”button时,所有标记的 UITableViewCell都将从UITableView中删除,以及相应的数据源对象。

要实现复选标记部分,可以考虑在UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryNone之间切换UITableViewCellaccessory属性。 在下面的UITableViewController委托方法中处理触摸:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; if (c.accessoryType == UITableViewCellAccessoryCheckmark) { [c setAccessoryType:UITableViewCellAccessoryNone]; } //else do the opposite } 

如果您想要更复杂的复选标记,您也可以查看关于自定义UITableViewCell的这篇文章 。

你可以设置一个主“删除”button两种方式:

  • IB方法
  • 程序化的方法

在任何一种情况下,当按下主“删除”button时,最终必须调用一个方法。 该方法只需要遍历UITableViewCells中的UITableView并确定哪些标记。 如果标记,删除它们。 假设只有一个部分:

 NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init]; for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) { NSIndexPath *p = [NSIndexPath indexPathWithIndex:i]; if ([[tableView cellForRowAtIndexPath:p] accessoryType] == UITableViewCellAccessoryCheckmark) { [cellIndicesToBeDeleted addObject:p]; /* perform deletion on data source object here with i as the index for whatever array-like structure you're using to house the data objects behind your UITableViewCells */ } } [tableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted withRowAnimation:UITableViewRowAnimationLeft]; [cellIndicesToBeDeleted release]; 

假设“编辑”是指“删除单个UITableViewCell ”或“移动单个UITableViewCell ”,则可以在UITableViewController实现以下方法:

 - (void)viewDidLoad { [super viewDidLoad]; // This line gives you the Edit button that automatically comes with a UITableView // You'll need to make sure you are showing the UINavigationBar for this button to appear // Of course, you could use other buttons/@selectors to handle this too self.navigationItem.rightBarButtonItem = self.editButtonItem; } // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //perform similar delete action as above but for one cell } } - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { //handle movement of UITableViewCells here //UITableView cells don't just swap places; one moves directly to an index, others shift by 1 position. } 

你可以把1 UIButton叫它“编辑”,并把它连接到IBAction。 在IBAction写,所以你将能够按照您的要求。

  -(IBAction)editTableForDeletingRow { [yourUITableViewNmae setEditing:editing animated:YES]; } 

这将在左上angular添加圆形的红色button,你可以点击那个删除button将出现点击,行将被删除。

你可以实现UITableView的委托方法如下。

  -(UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath { //Do needed stuff here. Like removing values from stored NSMutableArray or UITableView datasource } 

希望能帮助到你。

你想要寻找deleteRowsAtIndexPath ,所有的代码挤在[yourTable beginUpdates][yourTable endUpdates];

我发现代码非常好,请使用此代码来实现Tutorials Link的某些function