编辑&同时删除UITableView中的多行 [英] Edit & delete multiple rows in UITableView simultaneously

查看:61
本文介绍了编辑&同时删除UITableView中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我需要删除表格中的多行,编辑表格并在表格旁边显示一个复选框。选中后,表格单元格将被删除。它就像iPhone消息应用程序。我怎么能这样做,请帮助我。

In my app I need to delete multiple rows in a table, edit the table and get a check box beside the table. When checked then the table cells are deleted. It is like the iPhone message app. How can I do this, please help me.

推荐答案

如果我理解你的问题,你基本上想要标记 UITableViewCell 以某种方式(复选标记);然后,当用户点击主删除按钮时,所有标记 UITableViewCell 将从 UITableView中删除及其对应的数据源对象。

If I understand your question correctly, you essentially want to mark UITableViewCells in some way (a checkmark); then, when the user taps a master "Delete" button, all marked UITableViewCells are deleted from the UITableView along with their corresponding data source objects.

要实现 checkmark 部分,您可以考虑在<$ c $之间切换c> UITableViewCellAccessoryCheckmark 和 UITableViewCellAccessoryNone 用于 UITableViewCell 附件属性。处理以下 UITableViewController 委托方法中的触摸:

To implement the checkmark portion, you might consider toggling between UITableViewCellAccessoryCheckmark and UITableViewCellAccessoryNone for the UITableViewCell's accessory property. Handle touches in the following UITableViewController delegate method:

- (void)tableView:(UITableView *)tableView
  didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

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

}

您也可以查看此帖子关于自定义 UITableViewCell s如果你想要一个更复杂的复选标记

You might also look at this post regarding custom UITableViewCells if you're wanting a more complex checkmark.

你可以通过两种方式设置主删除按钮:

You can set up a master "Delete" button two ways:

  • The IB approach
  • The programmatic approach

在任何一种情况下,最终必须在主人时调用一个方法删除按钮被按下。该方法只需要遍历 UITableView 中的 UITableViewCells ,并确定标记了哪些。如果已标记,请将其删除。假设只有一个部分:

In either case, eventually a method must be called when the master "Delete" button is pressed. That method just needs to loop through the UITableViewCells in the UITableView and determined which ones are marked. If marked, delete them. Assuming just one section:

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 中实现以下方法:

Assuming by "edit" you mean "delete a single UITableViewCell" or "move a single UITableViewCell," you can implement the following methods in the 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. 
}

这篇关于编辑&amp;同时删除UITableView中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆