检测 TableViewController 上点击的 CustomCell Item 的索引

Posted

技术标签:

【中文标题】检测 TableViewController 上点击的 CustomCell Item 的索引【英文标题】:Detect the index of CustomCell Item clicked on TableViewController 【发布时间】:2015-12-02 16:26:38 【问题描述】:

我有一个自定义单元格,其中有两个文本字段和一个标签。我添加了一个TapGestuRecognizer 来捕获标签上的用户点击事件。但是,我想知道如何检测选择标签的 CustomCell 的索引?

var tapGesture = new UITapGestureRecognizer (Tap);
myLabel.AddGestureRecognizer (tapGesture);
myLabel.UserInteractionEnabled = true;

public void Tap(UITapGestureRecognizer r) 

    var vc = new myViewController (this);
    var nc = new NavigationController (vc);
    nc.PreferredContentSize = new System.Drawing.SizeF (200, 300);
    popupoverController = new UIPopoverController (nc);
    popupoverController.PresentFromRect (myLabel.Bounds, myLabel, UIPopoverArrowDirection.Any, true);

【问题讨论】:

***.com/a/13723853/826716 【参考方案1】:

如果手势识别器属于表格视图单元格,请将 indexPath 属性添加到表格视图单元格子类:

@property (nonatomic, strong) NSIndexPath *indexPath;

然后在返回单元格之前将其设置在cellForRowAtIndexPath: 方法中。

然后根据self.indexPath 属性处理表格视图单元格类中的触摸。

同样,你也可以添加一个指向“父”表视图控制器的指针(这次是weak,以免创建循环引用),然后你可以访问它的公共属性,就像你的数组一样'重新填充您的表格视图。

【讨论】:

【参考方案2】:

将触摸点(或 CGPointZero,它真的没关系)从您的标签(您可以从您的点击识别器获取它 - 它具有 view 属性)转换为表格视图坐标

func convertPoint(_ point: CGPoint, toView view: UIView?) -> CGPoint

或者,您可以为此使用 UITapGestureRecognizer 方法

func locationInView(_ view: UIView?) -> CGPoint

然后找到该点cell的indexPath

func indexPathForRowAtPoint(_ point: CGPoint) -> NSIndexPath?

文档:Converting Between View Coordinate Systems。

【讨论】:

以上是关于检测 TableViewController 上点击的 CustomCell Item 的索引的主要内容,如果未能解决你的问题,请参考以下文章

从 tableviewcontroller 加载 tableviewcontroller 不起作用

我无法将 TableViewCell 按钮连接到 TableViewController!如何将 TableViewCell 中的值连接到 TableViewController?

将 TableViewController 添加到现有项目

霍夫变换

RxSwift 以及如何制作简单的 TableViewController?

XCode 7 - TableViewController 错误