ios 同时编辑和删除 UITableView 中的多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6227168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:12:39  来源:igfitidea点击:

Edit & delete multiple rows in UITableView simultaneously

iphoneobjective-ciosuitableviewios4

提问by simbesi.com

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.

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

回答by Jacob Barnard

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

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

To implement the checkmarkportion, you might consider toggling between UITableViewCellAccessoryCheckmarkand UITableViewCellAccessoryNonefor the UITableViewCell's accessoryproperty. Handle touches in the following UITableViewControllerdelegate method:

为了实现对号部分,你可能会考虑之间切换UITableViewCellAccessoryCheckmark,并UITableViewCellAccessoryNoneUITableViewCellaccessory财产。在以下UITableViewController委托方法中处理触摸:

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

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

}

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

如果您想要更复杂的checkmark ,您也可以查看有关 customUITableViewCell这篇文章

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

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

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

在任何一种情况下,最终都必须在按下主“删除”按钮时调用一个方法。该方法只需要遍历UITableViewCellsinUITableView并确定标记了哪些。如果标记,删除它们。假设只有一节:

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];

Assuming by "edit" you mean "delete a single UITableViewCell" or "move a single UITableViewCell," you can implement the following methods in the UITableViewController:

假设“编辑”的意思是“删除单个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. 
}

回答by Janak Nirmal

You can put 1 UIButton lets call it "EDIT" and wire up it to IBAction. In IBAction write so you will be able to do as per your requirement.

您可以将 1 个 UIButton 称为“编辑”并将其连接到 IBAction。在 IBAction 中编写,这样您就可以按照您的要求进行操作。

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

This will add round red buttons on the left hand corner and you can click on that Delete button will appear click on that and row will be deleted.

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

You can implement delegate method of UITableView as following.

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

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

Hope it helps.

希望能帮助到你。

回答by theiOSDude

you want to be looking for deleteRowsAtIndexPath, with all your code squeezed between [yourTable beginUpdates]& [yourTable endUpdates];

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

回答by nivritgupta

i found very nice Code Here Please use this code to implementing the certain functionality Tutorials Link

我在这里找到了非常好的代码请使用此代码来实现某些功能 教程链接