iPhone Storyboard Editing a table view

前端 未结 7 1377
囚心锁ツ
囚心锁ツ 2021-02-06 04:15

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 lik

相关标签:
7条回答
  • 2021-02-06 04:54

    In case that you have UIViewController and inside this you added a UITableVIew. If you want to add an edit UIBarButton in order to interact with UITableView, try:

    Add this line...

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        ...
    
        self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
        ...
    }
    

    and this method

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
        [super setEditing:editing animated:animated];
        [self.myListTableView setEditing:editing animated:animated];
    
        if(self.myListTableView.editing) {
            NSLog(@"editMode on");
        } else {
            NSLog(@"editMode off");
        }
    }
    

    where

    @property (weak, nonatomic) IBOutlet UITableView *myListTableView;
    
    0 讨论(0)
  • 2021-02-06 05:03

    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];
        } 
    }
    
    0 讨论(0)
  • 2021-02-06 05:03

    one can use the dumb, not working Edit button from the Storyboard editor and then programmatically replace it with the UIViewController.editButtonItem.

    in viewDidLoad:

     NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
    
    [toolbarItems replaceObjectAtIndex:0 withObject:self.editButtonItem];
    
    [self setToolbarItems:toolbarItems];    
    

    this code assumes one has added the dumb Edit button as the leftmost item on the toolbar in the Storyboard.

    0 讨论(0)
  • 2021-02-06 05:06

    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

    0 讨论(0)
  • 2021-02-06 05:07

    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...

    0 讨论(0)
  • 2021-02-06 05:10

    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.

    0 讨论(0)
提交回复
热议问题