11

I have a UIViewController class, with a tableView. In viewDidLoad:

UIBarButtonItem *editIcon = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemEdit
                    target:self
                    action:@selector(toggleEditMode)] autorelease];

In te method 'toggleEditMode':

-(void)toggleEditMode{
if(self.theTable.editing) {
    [theTable setEditing:NO animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else if ([callsArray count]!=0){
    [theTable setEditing:YES animated:YES];
    [self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}

}

The problem is that the Edit button does not change do 'DONE'. What's missing? I have all the methods declared:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

Thanks,

RL

Rui Lopes
  • 2,562
  • 6
  • 34
  • 49

3 Answers3

22

Why not use -editButtonItem of UIViewController directly ? And override -setEditing:animated: method.

// Assign the system's edit button, 
// it will change style when different edit status.
self.navigationItem.rightBarButtonItem = self.editButtonItem;

// The editButtonItem will invoke this method.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];

    if (editing) {
         // Execute tasks for editing status
    } else {
         // Execute tasks for non-editing status.
    }
}
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • Perfect. Just changed if(editing) to if(theTable.editing) because de tableView is an IBOutlet. The class is not inherit from UITableViewController. Thanks, RL – Rui Lopes Mar 20 '11 at 14:17
  • 1
    Even though this is an old question it helped me realize I forgot the [super setEditing:editing animated:animated]; line, which is why mine wasn't working. ;) Thank you for posting this. – DataJock Jun 21 '13 at 17:12
2

It's because you're only setting the style of the button (not its text) when you switch modes. You need to do this (or equivalent):

-(void)toggleEditMode{
    self.navigationItem.rightBarButtonItem.title = self.tableView.editing ? @"Done" : @"Edit";
Zoran Simic
  • 10,293
  • 6
  • 33
  • 35
  • Why does not work like when a class is inherits from UITableViewController? Can't I assign the button to the table and work like that? – Rui Lopes Mar 19 '11 at 22:22
0

You sure callsArray is not empty ?

Place a breakpoint inside toggleEditMode and see what happens there.

Edit

Ok, after understanding your question, have a look at this thread

Community
  • 1
  • 1
Idan
  • 5,717
  • 10
  • 47
  • 84
  • yes, I'm sure. The table enter in Edit mode, and it works when I delete a row. That's not the problem. The problem is that the 'Edit' button does not change it's name do 'DONE' has does when the class is a UITableViewController class. Instead, my class is a UIViewController with a table in it. – Rui Lopes Mar 19 '11 at 22:02