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?
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];
}
This is being used in the case where on non-UITableViewController UIViewController has an outlet to the UITableView in question.
As mentioned above, you cannot change the values in the bar button item, particularly if it's a system item. If you desire, you can create the two items and have them available in ivars (like the UITableViewController). It's not really that much execution time to create a new one though, especially if it may never be used.
Swift 5
@IBOutlet var table: UITableView!
@IBAction func toggleTableEdit(sender:UIBarButtonItem) {
table.setEditing(!table.isEditing, animated: true)
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: (table.isEditing) ? .done : .edit, target: self, action: #selector(toggleTableEdit(sender:)))
}
@Nekto's answer is correct, you need to add this line to your viewDidLoad
implementation:
navigationItem.leftBarButtonItem = editButtonItem
However, this new "Edit" button can work not only with subclasses of UITableViewController
, but with any subclass of UIViewController
. You just have to implement the setEditing
method yourself.
For example, here is how it works in a UIViewController
with a tableView:
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
tableView.isEditing = editing // <-- toggle the tableView's editing mode
}