Borut Tomazin
Borut Tomazin

Reputation: 8138

How to set editing button on UITableView that is a subview on iOS

I have a UIViewController which has a UITableView as a child besides other elements. I want to add editing support on that tableView with this simple line:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

It works if I set this on UITableViewController view type, but obviously that does not work on tableView's that are subviews. Actually it shows the editing button but the edit action is not triggered.

Anyway if there is no elegant solution for that I will simply implement custom editing button.

Thanks!

Upvotes: 2

Views: 4431

Answers (4)

utopalex
utopalex

Reputation: 1354

You need to implement setEditing:animated yourself:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [self.tableView setEditing:editing animated:animated];
    [super setEditing:editing animated:animated];
}

Upvotes: 2

Apple.SL
Apple.SL

Reputation: 81

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"My View Controller";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonSelected:)] autorelease];

}

- (void) editButtonSelected: (id) sender
{
    if (tableView1.editing) {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonSelected:)] autorelease];
        [self.tableView1 setEditing:NO animated:YES];
    } else {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(editButtonSelected:)] autorelease];
        [self.tableView1 setEditing:YES animated:YES];

    }

}

Upvotes: 3

Borut Tomazin
Borut Tomazin

Reputation: 8138

I've just found a solution. Replacing self.editButtonItem with self.myTableViewController.editButtonItem like that:

self.navigationItem.rightBarButtonItem = self.myTableViewController.editButtonItem;

It works like a charm. Could it be more simple? :)

Upvotes: 1

hayden
hayden

Reputation: 293

self.navigationItem.leftBarButtonItem = self.editButtonItem;

Upvotes: 0

Related Questions