Search code examples
iosobjective-caccessory

Distinguish between accessoryView and Row on touch


I want users to get a different result if they click on an accessory image in a table cell rather than the row.

To capture their tap on the accessory, I am using following method:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;{
NSLog(@"accessory button tapped");
  }

In addition, I am not using the standard indicators. Instead, I am using custom images as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 [cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkmark.png"]]];

}

that overrides the standard accessory button and shows an image. Note: It shows the image no matter what the disclosure indicator is set to in storyboard, none, checkmark, disclosure indicator, etc.

However, currently, the

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;{
NSLog(@"accessory button tapped");
  }

method is not firing.

There are some answers on SO that suggest this method is not called if the accessory is an accessory view as opposed to accessory type.

Does anyone know if it is possible to use custom images and still get button tapped method to fire?


Solution

  • Observe a ; in your method signature even before body starts:

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;{
    }
    

    As a side note from Apple Documentation:

    Discussion The delegate usually responds to the tap on the disclosure button (the accessory view) by displaying a new view related to the selected row. This method is not called when an accessory view is set for the row at indexPath.

    This method is used for identifying touches on disclosure indicator and not on accessory view.

    In order to support your accessory view touches, create custom UITableViewCelland add an UITapGestureRecognizer on your accessory view to intercept the taps.