【问题标题】:Gesture for specific element in custom table view cell自定义表格视图单元格中特定元素的手势
【发布时间】:2014-05-26 19:22:36
【问题描述】:

我正在使用一个自定义表格,其中每个单元格都包含多个元素,其中一个是最喜欢的“星形”图标。

我想添加一个点击手势,这样当点击星形图标时,它会变为“不喜欢”图标(反之亦然)。

我尝试在 cellForRowAtIndexPath 中添加一个点击手势:

UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(favTapped)];
[[cell.contentView viewWithTag:1] addGestureRecognizer:tapRecognizer];

但点击图标只会调用 didSelectRowAtIndexPath(显然)。

不会调用我的选择器“favTapped”。

知道怎么做吗?

谢谢!

【问题讨论】:

  • 星形图标是否启用了用户交互?
  • 也许只是将 UIButton 作为子视图添加到单元格中?
  • 感谢 DrBeardface!,错过了 :) 启用了用户交互,它现在可以工作了。

标签: ios objective-c uitableview uitapgesturerecognizer


【解决方案1】:

您需要添加 GestureRecognizer 作为自定义单元类的属性。

在您的 customCell.h 中:

' @property (weak, nonatomic ) UIButton *yourButton; // or swipeGesture'

tableView:cellForRowAtIndexPatch:

   [cell.yourButton addTarget:self action:@selector(yourMethod) forControlEvents:yourControlEvent]; // also aviable for gestures

还验证是否启用了用户交互(在您的界面构建器中,您也可以在您自己的类中覆盖它或)。最简单的方法是在 tableView:cellForRowAtIndexPatch 中编写: [cell.contentView setUserInteractionEnabled:YES];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多