2011-06-03 47 views
6

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

+0

可能重复的[从uitableview和删除选择多行](http://stackoverflow.com/questi ons/4954393/select-multiple-rows-from-uitableview-and-delete) – 2011-06-03 18:05:29

+0

其他重复项:[1](http://stackoverflow.com/questions/6222661/how-to-delete-multiple-rows-in- table-view)[2](http://stackoverflow.com/questions/2727302/)[3](http://stackoverflow.com/questions/2949488/)[4](http://stackoverflow.com/问题/ 5973756 /) – 2011-06-03 18:06:24

回答

18

如果我正确地理解你的问题,你基本上想以某种方式(复选标记)标记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 

} 

look at this post可能关于定制UITableViewCell■如果你想要一个更复杂的对号

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

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

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

这就是我所说的“读心”。谢谢。 – Flar 2014-06-15 19:25:59

+0

宏伟的答案。 – aejhyun 2016-05-10 11:44:06

1

要寻找deleteRowsAtIndexPath,与所有你的代码挤[yourTable beginUpdates]之间& [yourTable endUpdates];

2

你可以把1的UIButton可以称之为“编辑”来包装它IBAction为。在IBAction写,所以你将能够按照您的要求。

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

这将在左上角添加圆形红色按钮,您可以点击该按钮将出现点击,行将被删除。

您可以按如下方式实现UITableView的委托方法。

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

希望它有帮助。

相关问题