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 ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…