iPhone Storyboard Editing a table view

12,366

Solution 1

I just started using Storyboards, so I also wanted to use the Storyboard to add my Edit button. It is annoying to have taken the time to learn how to use a new tool but find you need a roll of duct tape to patch up the holes.

You can get it to work, but need to add a Custom button. In the Attributes inspector make sure the Identifier is Custom and the title is Edit.

Then add something like this in your .m

- (IBAction)setEditMode:(UIBarButtonItem *)sender {
    if (self.editing) {
        sender.title = @"Edit";
        [super setEditing:NO animated:YES];
    } else {
        sender.title = @"Done";
        [super setEditing:YES animated:YES];
    } 
}

Have your Custom Edit button call the setEditMode method.

Can only hope they will fix the implementation of the Edit button in the Storyboard editor in the future.

Solution 2

I think that also with Storyboard, the only way (for sure, the easiest one) to implement a working edit/done button, is to use the following code:

- (void)viewDidLoad
{
[super viewDidLoad];
...
//set the edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...

This is the solution that Apple itself implements if you select a "Master-Detail Application" template for your project.

Probably Storyboard is still not perfect, and hopefully it will be improved from Apple in next releases...

Solution 3

To summarize:

The Button, returned by UIViewController.editButtonItem is a special toggling button with special behavior that calls - (void)setEditing:(BOOL)editing animated:(BOOL)animated if pressed.

The Button, returned by UINavigationController.editButtonItem is a simple Button, just labeled with "Edit".

The Storyboard allows to select the latter one.

Solution 4

If you are using the navigation controller to push to the view controller, simply set self.navigationItem.rightBarButtonItem = self.editButtonItem;, which will put the default Edit button in the right. If the navigation bar is not visible, call self.navigationController.navigationBarHidden = NO;. Those would be called in the viewDidLoad method, or something similar. Then in order to get the tableView to respond to the edit call, use the following method:

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

That should do what you want it to do. If you have any issues, just say so and we can narrow down the details

Solution 5

To add to @Graham answer, you might also want to change the style so you can have the "Done" button style (the blue color). Something like this:

- (IBAction)setEditMode:(UIBarButtonItem *)sender {
    if (self.editing) {
        sender.title = @"Edit";
        sender.style = UIBarButtonItemStylePlain;
        [super setEditing:NO animated:YES];
    } else {
        sender.title = @"Done";
        sender.style = UIBarButtonItemStyleDone;
        [super setEditing:YES animated:YES];
    } 
}
Share:
12,366
markplee
Author by

markplee

Updated on July 04, 2022

Comments

  • markplee
    markplee almost 2 years

    I've been trying to learn the new Storyboard feature in Xcode and I've run into a problem with trying to set a UITableView to edit mode.

    So far my storyboard looks like this:

    NavigationController -> UIViewController (subclass with tableview property)

    I added a Navigation Item and a Bar Button item to the view controller scene, so I do see an edit button. It didn't do anything automagically, so I tried linking it's selector to the setEditing method of the tableview delegate. This did put it into editing mode. However, the edit button did not change to a "Done" button and so there is no way to get out of editing mode.

    Do I have to create another Navigation item for the Done button? How do I connect it so that it appears at the right time and works correctly?