Michel
Michel

Reputation: 11749

How to use commitEditingStyle? (UITableViewCell-UITableView)

I am looking at various possible ways to implement delete-edit-copy feature on the UITableViewCell elements of a UITableView.

The one I am trying now is based on the usage of the following methods:

        [tableView setEditing:YES animated:YES];

        - (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
    forRowAtIndexPath:(NSIndexPath *)indexPath;

I can see some advantages to use this approach, but there is one problem:

When I try it, I can make the red "Delete" button appear and work, but that is all.

Is there a mechanism by which I could also have "edit" and "copy" buttons ? Or is it just not possible?

Thanks for any helpful information.

Upvotes: 2

Views: 10558

Answers (2)

Bajaj
Bajaj

Reputation: 869

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [DummyData removeObjectAtIndex:indexPath.row];
        [_tableView reloadData];
    }       
}

Use this code. Hope it helps and please do the same with UITableViewCellEditingStyleInsert

Upvotes: 1

Matthias
Matthias

Reputation: 8180

EDIT

I just understood that you are referring to the cell, not the TableView. For the cell, the following styles are provided:

  • UITableViewCellEditingStyleDelete: The cell has the delete editing control; this control is a red circle enclosing a minus sign.
  • UITableViewCellEditingStyleInsert: The cell has the insert editing control; this control is a green circle enclosing a plus sign. You may use it for "copy".
  • UITableViewCellEditingStyleNone: The cell has no editing control.

Further styles you have to implement on your own.

Original Answer

You can use the editButtonItem method of UIViewController. It returns a bar button item that toggles its title and associated state between "Edit" and "Done" (but not "Copy"). The default button action already invokes the setEditing:animated: method.

If you want another state, your have to create your own BarButtonItem.

Upvotes: 3

Related Questions