سؤال

I have a custom accessory view (a UIButton) on my prototype cell in a storyboard. When I click that button the delegate method for accessory button tapped isn't called. If I use a standard disclosure button it is called just fine. I'm assuming I'm missing a hook up somewhere. Anybody know where?

هل كانت مفيدة؟

المحلول 3

I ended up working around this using a protocol for custom table cells.

CustomAccessoryTableCellDelegate.h

@protocol CustomAccessoryTableCellDelegate <NSObject>

     @required
     - (void) accessoryButtonTappedForCell: (UITableViewCell *) cell;

@end

CustomAccessoryViewTableCell.h

 @interface CustomAccessoryViewTableCell : UITableViewCell

      /** The custom accessory delegate. */
      @property (nonatomic, weak) id<CustomAccessoryTableCellDelegate> delegate;

 @end

CustomAccessoryViewTableCell.m

- (IBAction) buttonAction:(id)sender{

    if ([self.delegate respondsToSelector:@selector(accessoryButtonTappedForCell:)]) {
        [self.delegate accessoryButtonTappedForCell:self];
    }

}

Inside Table View Controller

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

    CustomAccessoryViewTableCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:@"MyCustomCell"];

    cell.label.text = @"Some Name";
    cell.delegate = self;

    return cell;
 }

#pragma mark - Custom Accessory View Delegate

- (void) accessoryButtonTappedForCell:(UITableViewCell *)cell{
    [self tableView:self.writerTable 
        accessoryButtonTappedForRowWithIndexPath: [self.writerTable
                                                      indexPathForCell:cell]];
}

نصائح أخرى

Guess 1: You don't really have a "custom accessory view" you have a UIButton which is placed where an accessory would go. This wouldn't fire the delegate accessory tapped button because it's not really an accessory.

Guess 2: You do have a real accessory view, but it's never getting the "tap" event because you have a UIButton in it which is eating the user interaction but not firing an action. In that case try adding a simple UIView instead.

Other than that, I'd need more information.

apple docs for the accessory delegate method says

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.

so it is not called for custom accessory views. it is a standard behaviour.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top