0

I have a custom cell where I have two textfields and one label. I have added a TapGestuRecognizer to capture user tap event on the label. However, I wonder how could I able to detect the index of CustomCell where label selected?

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);
}
casillas
  • 16,351
  • 19
  • 115
  • 215

2 Answers2

0

Convert point of touch (or CGPointZero, it really doesn't matter) from your label (you can obtain it from your tap recognizer - it has view property) to table view coordinates with

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

Alternatively, you can use method of UITapGestureRecognizer for this

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

And then find the indexPath of cell at that point

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

Docs: Converting Between View Coordinate Systems.

grandboum
  • 622
  • 4
  • 5
0

If the gesture recognizer belongs to the table view cell, add an indexPath property to your table view cell subclass :

@property (nonatomic, strong) NSIndexPath *indexPath;

then set it in the cellForRowAtIndexPath: method before returning the cell.

Then handle the touch in the table view cell class, based on the self.indexPath property.

In the same way, you can also add a pointer to the "parent" table view controller (weak this time, so as not to create circular references), then you can access its public properties like the array you're populating your table view with.

Cyrille
  • 25,014
  • 12
  • 67
  • 90