Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In my table view controller there is

self.navigationItem.leftBarButtonItem = self.editButtonItem;

which produces a regular edit/done button at the top left corner. Hence, once the user clicks "Edit", the button caption changes to "Done" and the table entries may be deleted or reordered. I would like to get notified once the user actually clicks "Done". Is there a hook for that?

Background: I'd like to persist the order of the entries i.e. next time the user pulls up this view I'd like to present the entries in the least recently used order.

like image 600
Marcel Stör Avatar asked Jul 01 '09 19:07

Marcel Stör


2 Answers

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");
    }
}
like image 134
dustin.b Avatar answered Nov 17 '22 01:11

dustin.b


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];
}
like image 3
Dia Kharrat Avatar answered Nov 17 '22 00:11

Dia Kharrat