简体   繁体   English

UITableView原型单元上的自定义附件视图未触发

[英]Custom Accessory View on UITableView Prototype Cell Not Firing

I have a custom accessory view (a UIButton) on my prototype cell in a storyboard. 我在情节提要中的原型单元上有一个自定义的附件视图(UIButton)。 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? 有人知道吗

Guess 1: You don't really have a "custom accessory view" you have a UIButton which is placed where an accessory would go. 猜测1:您实际上没有“自定义附件视图”,而是有一个UIButton ,该UIButton放置在要去附件的位置。 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. 猜想2:您确实有一个真实的附件视图,但是它从未遇到“轻击”事件,因为其中有一个UIButton正在吃掉用户交互但未触发动作。 In that case try adding a simple UIView instead. 在这种情况下,请尝试添加一个简单的UIView

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. 在indexPath处为行设置附件视图时,不会调用此方法。

so it is not called for custom accessory views. 因此自定义附件视图不被调用。 it is a standard behaviour. 这是一种标准行为。

I ended up working around this using a protocol for custom table cells. 最后,我使用了用于自定义表格单元格的协议来解决此问题。

CustomAccessoryTableCellDelegate.h CustomAccessoryTableCellDelegate.h

@protocol CustomAccessoryTableCellDelegate <NSObject>

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

@end

CustomAccessoryViewTableCell.h CustomAccessoryViewTableCell.h

 @interface CustomAccessoryViewTableCell : UITableViewCell

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

 @end

CustomAccessoryViewTableCell.m 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]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM