iPhone, hook for edit/done button click in table view

后端 未结 4 1589
说谎
说谎 2021-02-01 17:54

In my table view controller there is

self.navigationItem.leftBarButtonItem = self.editButtonItem;

which produces a regular edit/done button at

相关标签:
4条回答
  • 2021-02-01 18:18

    For those who don't want to override UITableView (e.g. if you're using UITableViewController), here's a simple and clean solution that I use. It basically involves creating your own edit button item and using the tableView's editing flag to track edit vs done. For icing on the cake, it shows a "+" button (instead of "Edit") when the table is empty for adding new items.

    - (void) updateEditButtonVisibility
    {
        // tableItems represents the data structure that s
        if ([tableItems count] > 0)
        {
            UIBarButtonSystemItem editButtonType = self.tableView.editing ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit;
            UIBarButtonItem *editButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:editButtonType
                                                                                            target:self
                                                                                            action:@selector(editButtonTouched)];
    
            self.navigationItem.rightBarButtonItem = editButtonItem;
            [editButtonItem release];
        }
        else
        {
            UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self
                                                                                           action:@selector(addButtonTouched)];
            self.navigationItem.rightBarButtonItem = addButtonItem;
            [addButtonItem release];
        }
    }
    
    - (void) editButtonTouched
    {
        // edit/done button has been touched
    
        [self.tableView setEditing:!self.tableView.editing animated:YES];
        [self updateEditButtonVisibility];
    }
    
    - (void) addButtonTouched
    {
        // logic to allow user to add new items goes here
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [self updateEditButtonVisibility];
    }
    
    0 讨论(0)
  • 2021-02-01 18:22

    for those who are still interesed in this question (or answer :P)

    UITableView API

    revealed that there is a - (void)setEditing:(BOOL)editing animated:(BOOL)animate method these method is called every time this edit/done button is pressed. you have to simply check by the (BOOL)editing parameter wich one was used. last but not least you have to call the proper method from the originally edit/done button.

    simply add this method to your uitableview class

    - (void)setEditing:(BOOL)editing animated:(BOOL)animate
    {
        [super setEditing:editing animated:animate];
        if(editing)
        {
            NSLog(@"editMode on");
        }
        else
        {
            NSLog(@"Done leave editmode");
        }
    }
    
    0 讨论(0)
  • 2021-02-01 18:25

    It is possible to change the action. After edit button clicked it shows delete button, instead it is possible to show reject/verify/modify buttons. And change corresponding action instead of delete option

    Thanks Mindus

    0 讨论(0)
  • 2021-02-01 18:30

    This is the standard way to get notified when a bar button has been pushed:

    self.editButtonItem.target = self;
    self.editButtonItem.action = @selector(buttonPushed:);
    
    ...
    
    - (void) buttonPushed:(id)sender
    {
    // do stuff here
    }
    
    0 讨论(0)
提交回复
热议问题