rplankenhorn
rplankenhorn

Reputation: 2084

UITableView Edit/Done events

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

What I want to do is detect that the table is editing and then display an extra row that says "Add new item" or something like that.

I tried adding the "Add New Item" row at the end of the array when the View Controller is loaded and then depending on whether [tableView isEditing] is true or not, either return [array count] (for when I am editing) or [array count] - 1 (for when I am not editing).

Any ideas? What is the way Apple edits items in the table and allows for deletion?

Upvotes: 7

Views: 2929

Answers (2)

rplankenhorn
rplankenhorn

Reputation: 2084

I found it. Override this method:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];

    // do something
}

Upvotes: 6

AzaFromKaza
AzaFromKaza

Reputation: 768

What you can do is to add an IBAction as a selector to your editButton. When editButton is tapped that 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 didn't use standard self.editButtonItem then use [yourButton addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside]; And handle it as a UIButton* in editAction: method

Upvotes: 3

Related Questions