views:

2682

answers:

3

My edit button is placed in viewDidLoad:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

It shows up correctly on the nav bar, and tapping this button indeed change it to Done. However, no minus buttons show up in my table rows. Swiping a row, then tap Delete works, though.

Any ideas?

EDIT 1: Here's how I'm doing:

- (void)loadView {
tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
tableView.delegate = self;
tableView.dataSource = self;
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

self.view = tableView;
}

EDIT 2: My observation is that the edit and minus buttons display fine if my tableview is created in IB (RootViewController). The other two (or three) tableview are created by the aforemention code, so that might be the problem. Guess I'd have to dive in to isEditing, editing and whatnot.

A: 

You probably need to do:

self.tableView = tableView;

in your loadView method. That's the property that UITableViewController manipulates, and it gets set automatically if you're loading the view from a NIB. Since you're creating the view programatically, you need to set it explicitly.

Daniel Dickison
There's no need to do this. The most important thing to remember is to change the parent class from UIViewController to UITableViewController.
QAD
+5  A: 

Silly me. I forgot to change UIViewController (the class my view controller inherits from) to UITableViewController. Now, it works.

Without doing this, I would need to enable row editing manually like:

// in loadView
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(turnOnEditing)];

- (void)turnOnEditing {
[self.navigationItem.rightBarButtonItem release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(turnOffEditing)];
[self.tableView setEditing:YES animated:YES];
}

- (void)turnOffEditing {
[self.navigationItem.rightBarButtonItem release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(turnOnEditing)];
[self.tableView setEditing:NO animated:YES];
}
QAD
+3  A: 

If you don't subclass UITableViewController, there is a way to do it. Just implement setEditing:animated: in your UIViewController subclass as follows:

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

Note: replace "self.tableView" if needed...

Also add the "edit" buttonto the toolbar:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

And that's all!

Seba