accessoryButtonTappedForRowWithIndexPath: not getting called

后端 未结 8 1898
执念已碎
执念已碎 2020-12-05 13:01

I am creating a Detail disclosure button which is getting populated using an array.... However the accessoryButtonTappedForRowWithIndexPath: function is not being called in

相关标签:
8条回答
  • 2020-12-05 13:58

    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 ?

    0 讨论(0)
  • 2020-12-05 14:03

    Did you just click on the cell to select it or did you actually click on the accessory button indicator on the cell? It isn't clear from your question.

    accessoryButtonTappedForRowWithIndexPath is applicable when you click on the button icon within the cell and not when you select the cell.

    0 讨论(0)
提交回复
热议问题