Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessoryButtonTappedForRowWithIndexPath: not getting called

I am creating a Detail disclosure button which is getting populated using an array.... However the accessoryButtonTappedForRowWithIndexPath: function is not being called in my class. It is a TableviewDelegate and TableviewDatasource delegate.

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{     NSLog(@"reaching accessoryButtonTappedForRowWithIndexPath:");     [self performSegueWithIdentifier:@"modaltodetails" sender:[self.eventsTable cellForRowAtIndexPath:indexPath]]; } 

The NSLog isnt printing to console which leads me to believe the function isnt being called... This is of course when I select on a cell. A screenshot below shows how I have my cell setup.

enter image description here

like image 846
jimbob Avatar asked Sep 06 '12 09:09

jimbob


Video Answer


1 Answers

The doc says that the method tableView:accessoryButtonTappedForRowWithIndexPath: is not called when an accessory view is set for the row at indexPath. The method is only called when the accessoryView property is nil and when one uses and set the accessoryType property to display a built-in accessory view.

As I understand it, accessoryView and accessoryType are mutually exclusive. When using accessoryType, the system will call tableView:accessoryButtonTappedForRowWithIndexPath: as expected, but you have to handle the other case by yourself.

The way Apple does this is shown in the Accessory sample project of the SDK. In the cellForRowAtIndexPath method of the dataSource delegate, they set a target/action to a custom accessory button. Since one can't pass the indexPath to the action, they call an auxiliary method to retrieve the corresponding indexPath and they pass the result to the delegate method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{     ...      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];     ...      // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet     [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];     ...     cell.accessoryView = button;      return cell; }   - (void)checkButtonTapped:(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];     } } 

For some reason, your setup seems to fall in the accessoryView case. Have you tried to set the accessoryType with code instead of using the Interface Builder ?

like image 140
Tom Avatar answered Oct 05 '22 15:10

Tom