How can I implement a button on the navigation bar whereby the user would be able to reorder & delete rows of a UITableView?
Do I have to create my own toolbar button to have the Edit/Done button for my UITableView?
Just add this line in viewDidLoad
of your UITableViewController
self.navigationItem.leftBarButtonItem = self.editButtonItem;
It will work if your table view superview is UINavigationController
. This line will add button that will push table in edit mode and out of it.
What's generally done is you create your own custom BarbuttonItem and then assign this button as right navigation bar button item:
UIBarButtonItem *barButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"Edit"
style:UIBarButtonItemStylePlain
target:self
action:@selector(toggleEdit)];
self.navigationItem.rightBarButtonItem = barButtonItem;
[barButtonItem release];
Here's the toggleEdit method:
-(void)toggleEdit{
[self.tableView setEditing:!self.tableView.editing animated:YES];
if (self.tableView.editing)
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
else
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
}
UIButton *btnname=[UIButton buttonWithType:UIButtonTypeSystem];
[btnname setFrame:CGRectMake(0,0,110,35)];
[btnname setFont:[UIFont boldSystemFontOfSize:18]];
[btnname setTitle: @"Delete" forState: UIControlStateNormal];
[btnname setTitleColor:UIColorFromRGB(0xCC0707) forState:UIControlStateNormal];
btnname.backgroundColor=UIColorFromRGB(0xE6E7E8);
btnname.showsTouchWhenHighlighted = YES;
[btnname addTarget:self
action:@selector(toggleEdit)
forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btnname];
self.navigationItem.rightBarButtonItem = barItem;
-(void)toggleEdit{
[self.tableView setEditing:!self.tableView.editing animated:YES];
if (self.tableView.editing)
[btnname setTitle: @"Done" forState: UIControlStateNormal];
else
[btnname setTitle: @"Delete" forState: UIControlStateNormal];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With