0

I have a tableviewcontroller with a uinavigation bar that has a barbuttonitem, called editBarButton. When editBarButton is pressed I want my tableview to be updated with a new button in each of the cells that says 'Edit'. What is the correct way to implement this?

- (void)onEditBarButtonPressed{
    //TODO: update cells
}
Joshua Abrams
  • 377
  • 1
  • 5
  • 17

2 Answers2

0

You have to overwrite the accessoryView attribute in your UITableViewCell with your Edit button:

Create a custom button to overwrite the current accesoryView:

- (UIButton *) makeDetailDisclosureButton
{
    UIButton * button = [UIButton yourEditButton];

[button addTarget: self
               action: @selector(accessoryButtonTapped:withEvent:)
     forControlEvents: UIControlEventTouchUpInside];

    return ( button );
}

Then the button will call this routine when it's done, which then feeds the standard UITableViewDelegate routine for accessory buttons:

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event
{
    NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: self.tableView]];
    if ( indexPath == nil )
        return;

    [self.tableView.delegate tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
}

You can see a similar question here: Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate

Community
  • 1
  • 1
3lvis
  • 4,190
  • 1
  • 30
  • 35
  • From what I can tell, this only adds a delete button to the cells. Is there a way to adapt this to have a custom button? – Joshua Abrams Nov 07 '11 at 15:15
0

I found a solution to my problem.

You should add a object level BOOL flag called editPressed. It should be set to NO in viewDidLoad.

When making each cell, add a button to each and set it to hidden if need be:

[button setHidden:!editPressed];

It is important to use the flag so that when new cells are made they will keep the buttons hidden if they should be or visible otherwise.

Then have a object level NSMutableArray * of the buttons in the view controller and add each button to it:

[buttons addObject:button];

When you want to show each button, just change the hidden state:

editPressed = YES;
for(int i = 0; i != [butttons count]; i++){
    [[buttons objectAtIndex:i] setHidden:!editPressed];
}

When you want to hide each button, once again, change the hidden state:

editPressed = NO;
for(int i = 0; i != [butttons count]; i++){
    [[buttons objectAtIndex:i] setHidden:!editPressed];
}
Joshua Abrams
  • 377
  • 1
  • 5
  • 17