Reputation: 6533
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.
Upvotes: 7
Views: 9097
Reputation: 992
To show and to activate the minus button at the left corner of each of the tableviewcell, do the following:
Add the following code to your viewdidload method:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//The above line is to show the "EDIT" and "DONE" button on the top right corner of your navigation bar.
And also add the following method to your .m file to show and animate the MINUS sign on the tableview cells.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
if(editing == YES) {
[self.tableView setEditing:editing animated:animated];
} else {
[self.tableView setEditing:NO animated:animated];
}
}
Upvotes: 2
Reputation: 624
I ran into the same issue. According to the docs for UITableViewController, it reads: UITableViewController implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.
See the last bullet point in the UITableViewController doc.
Upvotes: 0
Reputation: 1051
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!
Upvotes: 39
Reputation: 6533
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];
}
Upvotes: 10
Reputation: 21882
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.
Upvotes: 0