代码之家  ›  专栏  ›  技术社区  ›  Alex Gosselin

UITableViewCell中的自定义附件视图向表视图控制器发送消息的好方法是什么?

  •  1
  • Alex Gosselin  · 技术社区  · 14 年前

    我正在向UITableViewCell添加自定义附件视图(按钮),需要它在触摸表视图时通知表视图,但我无法确定如何与表视图通信按下了什么按钮。理想情况下,我想以某种方式调用这样的函数:

    [controller tableView:view didSelectCustomButtonAtIndexPath:indexPath usingCell:self];
    

    当按下自定义视图按钮时。

    对不起,如果这有点含糊,我真的不知道该如何解释清楚。我主要在寻找如何模拟TableView:didselectrowatindexpath的实现,而不必对UITableViewCell进行子类。

    谢谢你的帮助。

    1 回复  |  直到 14 年前
        1
  •  3
  •   David Kanarek    14 年前

    当我不得不这样做的时候,我是这样做的:

    //when adding accessoryView to cell in cellForRowAtIndexPath:
    UIButton *b = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [b addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.accessoryView = b;
    
    //the handling method
    - (void)doAction:(id)sender {
        NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell *)[sender superview]];
        // code goes here, indexPath tells you what row was touched
    }
    

    应使用uibutton以外的控件。