2015-12-02 30 views
0

我有一個自定義單元格,其中有兩個文本字段和一個標籤。我添加了一個TapGestuRecognizer來捕獲標籤上的用戶點按事件。但是,我不知道如何能夠檢測標籤選擇的CustomCell的索引?觸摸(或CGPointZero,它真的並不重要)從標籤(你可以從你的水龍頭識別獲得它 - 它有view財產)的檢測單擊在TableViewController上的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); 
} 
+0

http://stackoverflow.com/a/13723853/826716 – bteapot

回答

0

轉換點表視圖與

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

或者,你可以使用UITapGestureRecognizer的方法,該

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

,然後找到小區的indexPath在這一點上

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

Docs:Converting Between View Coordinate Systems。返回小區之前

@property (nonatomic, strong) NSIndexPath *indexPath; 

然後將其設置在cellForRowAtIndexPath:方法:

0

如果手勢識別屬於表視圖電池,indexPath屬性添加到您的表格視圖單元格的子類。

然後根據self.indexPath屬性處理表視圖單元類中的觸摸。同樣,你也可以添加一個指向「父」表視圖控制器的指針(此時爲weak,以免創建循環引用),那麼你可以訪問它的公共屬性,比如你所在的數組。用你的表格視圖填充。