0

swift: how to get the indexpath.row when a button in a cell is tapped?

This link is an answer for when a button is tapped, and if my question is not possible I'll just use a button and put the image on it. I was just wondering if it is possible to this with tapping a UIImageView instead of a button. I tried the exact answer with a UIImageView instead of a UIButton and I got this error

"fatal error: unexpectedly found nil while unwrapping an Optional value".

cell.imagePosted is a UIImageView

let tapGR = UITapGestureRecognizer(target: self,action:Selector("imageTapped:"))
cell.imagePosted.userInteractionEnabled = true
cell.imagePosted.addGestureRecognizer(tapGR);

func imageTapped(img: AnyObject)
{
    if let imgView = img as? UIImageView {

        if let superView = imgView.superview {

            if let cell = superView.superview as? CellCustom {

                indexPath2 = self.tableView.indexPathForCell(cell)

            }      
       }       
   }

   print(indexPath2.row)       
}
Community
  • 1
  • 1

4 Answers4

1

this might help you,

add an UITapGestureRecognizer to UIImageView You can store indexpath.row in tag property of UIImageView and access that tag on UITapGestureRecognizer event

for example (Objective-C) :

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
    tap.cancelsTouchesInView = YES;
    tap.numberOfTapsRequired = 1;
    [cell.imageView addGestureRecognizer:tap];

    cell.imageView.tag = indexPath.row;

and get indexpath.row

-(void)handleImageTap:(UITapGestureRecognizer *)gestureRecognizer{
    UIView* view = gestureRecognizer.view;
    CGPoint loc = [gestureRecognizer locationInView:view];
    NSInteger indexpath = [view hitTest:loc withEvent:nil].tag;
    NSLog(@"%ld",(long)indexpath);
}
Madan gupta
  • 668
  • 6
  • 19
0

You can create a protocol with a method like imageViewInCellTapped(cell:YourCellType)and a delegate property in the cell. In cellForRowAtIndexPath set the controller the delegate of each cell and implement the method from the protocol. When something happens in your cell like a button or image is tapped you can call delegate?.imageViewInCellTapped(self) where self is the cell then in your controller where this method is implemented you get the index using indexPathForCell method of the tableView.

Lucian Boboc
  • 480
  • 2
  • 6
  • Thanks for the answer Lucian, but Madan's answer worked for me, and also I'm a beginner and I'm not familiar with protocols yet. The tutorial I did on Udemy didn't mention protocols for some reason. – Miguel Bravo Feb 19 '16 at 10:26
  • 1
    You need be aware of cell reuse, you could end up with the wrong value for the indexPath if the cell is reused. – Lucian Boboc Feb 19 '16 at 10:29
  • Do you mind giving me an example of cell reuse? So I would know not to do it. – Miguel Bravo Feb 19 '16 at 10:45
  • A cell is reused automatically while scrolling, you can use prepareForReuse in a cell to reset its state. – Lucian Boboc Feb 19 '16 at 10:46
0

Here is Madan's code in swift if anybody is interested:

    func imageTapped(gestureRecognizer: UITapGestureRecognizer) {

    var view: UIView!
    var loc: CGPoint!
    view = gestureRecognizer.view
    loc = gestureRecognizer.locationInView(view)

    var indexPath: NSInteger!

    indexPath = (view.hitTest(loc, withEvent: nil)?.tag)!

    print(indexPath)

}
0

Hope this code helps to get indexpath for tapped cell swift 3:

  func nameTapped(_ sender:UITapGestureRecognizer)
{
    var pointVAlue = CGPoint()
    pointVAlue = sender.location(in: infoTable)

    var indexPath = IndexPath()
    indexPath = infoTable.indexPathForRow(at: pointVAlue)!

    print(indexPath.row)

   /* if indexPath != nil {

        let userEnt  = ratingArray.object(at: indexPath.row)as! RateEntity

        let detailVc = AssociateDetailViewController()          

        if ((userEnt.userId as String).isEmpty == false )
        {
            detailVc.m_otherUserId = userEnt.userId as String
            self.navController!.pushViewController(detailVc, animated: true)
        }
    }*/

}
soumya
  • 3,801
  • 9
  • 35
  • 69