Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting an editButtonItem to a UITableView

I have a UIViewController which adds a UITableView and a UIToolbar to its view. Unfortunately I can not use a UITableViewController in this instance.

I have added the view controller's editButtonItem to the toolbar. I need to replicate the default behavior of the edit button on a UITableViewController - the edit button should be tied to the tableView and switch it in/out of edit mode.

I have overridden the UIViewController's setEditing to include the line:

[_tableView setEditing:editing animated:animated];

and this works for the most part - the table view successfully enters and exits edit mode when the editButtonItem is used.

However, there is at least one issue. On a UITableViewController, the edit button switches to 'done' when a the user slides across a row in the table. 'Done' then returns that row to it's non-editing version. Also, touching outside the row returns it to view mode, and reverts the edit button to 'Done'.

My questions:

  1. How can I replicate this behavior, such that the Edit button changes state when an individual row enters/exits edit mode?
  2. What other behaviors of the UITableViewController's editButtonItem might I need to replicate?
like image 929
Ben Packard Avatar asked Oct 15 '12 15:10

Ben Packard


1 Answers

I have such a case in my own app. I implemented the following table view delegate methods:

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    [self setEditing:YES animated:YES];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    [self setEditing:NO animated:YES];
}
like image 140
rmaddy Avatar answered Oct 12 '22 00:10

rmaddy