UITableView Edit / Done Events

Is there a notification or delegate method that I can use to detect when a table view goes into edit state?

What I want to do is find that the table is being edited, and then displays an extra line that says “Add new item” or something like that.

I tried to add the line “Add a new element” at the end of the array when loading the view controller, and then depending on whether [tableView isEditing] is true or not, or return [number of arrays] (when I'm editing) or [number of arrays] - 1 (for those cases when I do not edit).

Any ideas? How does Apple edit items in a table and allow it to be deleted?

+7
source share
2 answers

I found him. Override this method:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; // do something } 
+6
source

What you can do is add IBAction as a selector to your editButton. When editButton is used, this method will be called. Example:

 -(void)viewDidLoad { // ... [self.editButtonItem setAction:@selector(editAction:)]; [self.navigationItem setRightBarButtonItem: self.editButtonItem]; // .. your code } -(IBAction)editAction:(id)sender { UIBarButtonItem * button = ((UIBarButtonItem*)sender); if (!self.tableView.editing) { [self.tableView setEditing:YES animated:YES]; [button setTitle:@"Done"]; // do your stuff... } else { [button setTitle:@"Edit"]; [self.tableView setEditing:NO animated:YES]; // do your stuff... } } 

If you have your own UIButton and you are not using the standard self.editButtonItem, then use [yourButton addTarget: the action itself: @selector (editAction :) forControlEvents: UIControlEventTouchUpInside]; And treat it like UIButton * in editAction: method

+3
source

All Articles