4

I need to style the default delete button on a tableview cell in objective c, ios5. The general idea is the assumption that you can do something like this:

    UIImage *addImage = [UIImage imageNamed:@"greenButtonDark.png"];
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(0, 0, addImage.size.width, addImage.size.height);

    [addButton setImage:addImage forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(pushAddItem) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *addBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton] ;

    self.navigationItem.rightBarButtonItem = addBarButtonItem;

The above code is called in the viewDidLoad method and overwrites the right button in the header. I assume there is something comparable for any button the system adds by default but I don't know how to access this particular one.

If I am failing to articulate this, the delete button I refer to is the one automatically generated with this code...

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        //do something when someone hits delete
    }
}

If I need to clarify anything let me know. Thanks.

enter image description here

Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • Have you tried sub classing the UITableView? It *might* work. You would have to discover which method calls `- tableView:commitEditingStyle:forRowAtIndexPath:` Anyway, why would want to delete a bullet proof vest? – apple16 Jun 09 '12 at 01:26
  • No I haven't. It just seemed logical that one could access the default button in a similar way. ANyhow, I should hope you delete the bullet proof vest as it suggests you acquired it and no longer need it on your list. – Kai Qing Jun 09 '12 at 01:33
  • You can't access that button (except to change the title). What you _can_ do is use your own editing accessory view for the cell, but you then have some additional work to do to make "edit mode" delete mode, and implement swipe-to-delete etc. – jrturton Jun 27 '12 at 13:18
  • See http://stackoverflow.com/a/8021606/852828 for some links about that – jrturton Jun 27 '12 at 13:20

1 Answers1

0

Take a look at this answer iPhone UITableView - Delete Button, it should basically answer your question. In short you might need to subclass UITableViewCell and override the -(void)willTransitionToState:(UITableViewCellStateMask)state and -(void)didTransitionToState:(UITableViewCellStateMask)state and modify the button here.

Another alternative might be subclassing the UITableViewCell and implementing the delete button functionality yourself (recognize the swipe gesture, present the button and handle when it's pressed in a similar way the default method of UITableView delegate does).

Community
  • 1
  • 1
domkck
  • 1,146
  • 1
  • 9
  • 19