【问题标题】:Detect the index of CustomCell Item clicked on TableViewController检测 TableViewController 上点击的 CustomCell Item 的索引
【发布时间】: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);
}

【问题讨论】:

标签: ios uitableview xamarin custom-cell


【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多