且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编辑同时删除 UITableView 中的多行

更新时间:2023-12-03 12:32:10

如果我正确理解你的问题,你本质上是想以某种方式标记 UITableViewCells(a复选标记);然后,当用户点击主删除"按钮时,所有 标记 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 部分,您可以考虑在 UITableViewCellAccessoryCheckmarkUITableViewCellAccessoryNone 之间切换 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.

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:

在任何一种情况下,最终都必须在按下主删除"按钮时调用一个方法.该方法只需要遍历 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. 
}