Custom image for the detail disclosure button

There are some times that you want to have two targets from a cell. One solution is to navigate to different UIViewController when you tap in the UITableViewCell and to a different when you tap to its detail disclosure button.

I’m sure that in a situation like that you want to spice up a little the design by adding a custom image as a detail disclose button. Like this, the paw is the button…

iOS Simulator Screen shot Jul 26, 2014, 15.22.55

How to do it? Here you are…this is very easy.
Add to your prototype cell a UIButton.

customDisclosure

Then connect this with the following action

- (IBAction)accessoryButtonTapped:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    if (indexPath != nil) {
        [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
    }
}

In this action method implementation we take advantage of the event (Touch up inside).
Using the event we access the touch action, from this we can get the exact position as CGPoint.
Thereafter everything is easy, using UITableViewCell’s indexPathForRowAtPoint method returns the
correct indexPath.

In turn, in a successful tap, it triggers the following delegate method

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"EditPet" sender:indexPath];
}

and at this point we perform the requested seque.

Reference:
1. Changing the Image for a detail disclosure button

Leave a Reply

Your email address will not be published. Required fields are marked *