如何在editButtonItem中对“删除”进行操作?

时间:2021-07-30 20:23:07

I've in my tableview i've

我在我的桌面视图中

self.navigationItem.rightBarButtonItem = self.editButtonItem;

clicking on the minus button shows Delete button. I want to know how to give an action to this Delete button. Tableview shows a list of items from sqlite database.I'm using Xcode 4.2

单击减号按钮显示删除按钮。我想知道如何对此删除按钮进行操作。 Tableview显示了sqlite数据库中的项目列表。我正在使用Xcode 4.2

5 个解决方案

#1


4  

The delete button is already linked to your table view, and tapping it will send tableView:commitEditingStyle:forRowAtIndexPath: to your table view's data source. You do your deletion here.

删除按钮已链接到您的表视图,并点击它将发送tableView:commitEditingStyle:forRowAtIndexPath:到您的表视图的数据源。你在这里删除。

This method is normally included in the template for UITableViewController subclasses.

此方法通常包含在UITableViewController子类的模板中。

#2


1  

If you look in to the UITableViewDataSource Protocol Reference you will find in there tableView:commitEditingStyle:forRowAtIndexPath:.

如果你查看UITableViewDataSource协议参考,你会在那里找到tableView:commitEditingStyle:forRowAtIndexPath:。

Asks the data source to commit the insertion or deletion of a specified row in the receiver.

要求数据源提交插入或删除接收器中的指定行。

In this method you will get the index path for the cell to delete. You have to remove the corespondent element in four data source. And you also need to remove the table view cell.

在此方法中,您将获取要删除的单元格的索引路径。您必须删除四个数据源中的corespondent元素。您还需要删除表格视图单元格。

 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

#3


1  

When you press delete button it will automatically call this delegate method of UITableView:

当你按下删除按钮时,它会自动调用UITableView的这个委托方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath.
{
 // Check if it's a delete button or edit button...

    if (editingStyle == UITableViewCellEditingStyleDelete) 
   {

    // Update the array and table view.
    [eventsArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

    // Commit the change.

    NSError *error;

    if (![managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
}   

}

#4


1  

Implement tableView:commitEditingStyle:forRowAtIndexPath: in your table view data source (presumably your table view controller). See the docs on that method, or any of several Apple sample code projects (including what you get from the Xcode "master-detail app" template) for details & example usage.

在表视图数据源(可能是您的表视图控制器)中实现tableView:commitEditingStyle:forRowAtIndexPath:。有关详细信息和示例用法,请参阅该方法的文档或几个Apple示例代码项目中的任何一个(包括您从Xcode“master-detail app”模板获得的内容)。

#5


0  

I would add that you should make sure you are reloading the table data so it can update on the screen. Otherwise it will look like it didn't do anything.

我想补充一点,你应该确保你正在重新加载表数据,以便它可以在屏幕上更新。否则看起来它什么都没做。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myMutableArray removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];
}

#1


4  

The delete button is already linked to your table view, and tapping it will send tableView:commitEditingStyle:forRowAtIndexPath: to your table view's data source. You do your deletion here.

删除按钮已链接到您的表视图,并点击它将发送tableView:commitEditingStyle:forRowAtIndexPath:到您的表视图的数据源。你在这里删除。

This method is normally included in the template for UITableViewController subclasses.

此方法通常包含在UITableViewController子类的模板中。

#2


1  

If you look in to the UITableViewDataSource Protocol Reference you will find in there tableView:commitEditingStyle:forRowAtIndexPath:.

如果你查看UITableViewDataSource协议参考,你会在那里找到tableView:commitEditingStyle:forRowAtIndexPath:。

Asks the data source to commit the insertion or deletion of a specified row in the receiver.

要求数据源提交插入或删除接收器中的指定行。

In this method you will get the index path for the cell to delete. You have to remove the corespondent element in four data source. And you also need to remove the table view cell.

在此方法中,您将获取要删除的单元格的索引路径。您必须删除四个数据源中的corespondent元素。您还需要删除表格视图单元格。

 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

#3


1  

When you press delete button it will automatically call this delegate method of UITableView:

当你按下删除按钮时,它会自动调用UITableView的这个委托方法:

- (void)tableView:(UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath.
{
 // Check if it's a delete button or edit button...

    if (editingStyle == UITableViewCellEditingStyleDelete) 
   {

    // Update the array and table view.
    [eventsArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

    // Commit the change.

    NSError *error;

    if (![managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
}   

}

#4


1  

Implement tableView:commitEditingStyle:forRowAtIndexPath: in your table view data source (presumably your table view controller). See the docs on that method, or any of several Apple sample code projects (including what you get from the Xcode "master-detail app" template) for details & example usage.

在表视图数据源(可能是您的表视图控制器)中实现tableView:commitEditingStyle:forRowAtIndexPath:。有关详细信息和示例用法,请参阅该方法的文档或几个Apple示例代码项目中的任何一个(包括您从Xcode“master-detail app”模板获得的内容)。

#5


0  

I would add that you should make sure you are reloading the table data so it can update on the screen. Otherwise it will look like it didn't do anything.

我想补充一点,你应该确保你正在重新加载表数据,以便它可以在屏幕上更新。否则看起来它什么都没做。

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.myMutableArray removeObjectAtIndex:indexPath.row];
    [self.tableView reloadData];
}