How to Get Indexpath When Image Inside Cell Tapped

How to get indexPath when image inside cell tapped

1: set tag of ImageView in cellForRowAtIndexpath method , tag will be equal to indexPath.row

imgView.tag = indexPath.row

2: add a target to tapGuestureRecognizer attached on imageView

3: get tag of imageView in that method

let imgView = sender as! UIImageView
let tag = imgView.tag

4: get data accordingly ,and push

let next = self.storyBoard.instatiateViewController(WithIdentifer:"detailVC") 
next.data = datas[tag]
self.navigationController.pushViewController(next)

How to get indexPath from image gesture in table view's cell

Well, you need to change your code little bit instead of gesture.view you need to find location in your tableView

@objc func imageTapped(_ gesture:UITapGestureRecognizer){

guard let indexPath = myTableView.indexPathForRow(at: gesture.location(in: self.tableView)) else {
print("Error: indexPath)")
return
}

print("indexPath.row: \(indexPath.row)")

}

and if problem still persists, you can give tag to imageView like this

imageview.tag = indexPath.row //place it in cellForRow

//place below lines in gesture handler
guard let view = sender.view else {return}
let indexPath = IndexPath(row:view.tag, section: 0) //only works if section remains same/ unique

How to get the indexpath.row when a UIImageView in a cell is tapped?

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);
}

Get IndexPath of CollectionViewCell outside of Cell

Get the location of the tapGestureRecognizer in the collectionView and then the indexPath at the given location

let pointInCollectionView = tapGestureRecognizer.location(in: collectionView)
let indexPath = collectionView.indexPathForItem(at: pointInCollectionView)

How to get a image's indexpath in CollectionView in TableView?

You can do something like this:

func imageTapped(gesture: UITapGestureRecognizer) {
let location: CGPoint = gesture.location(in: tableView)
let ipath: IndexPath? = tableView.indexPathForRow(at: location)
let cellindex: UITableViewCell? = tableView.cellForRow(at: ipath ??
IndexPath(row: 0, section: 0))
}

How to get the indexPath of a UIButton in a UITableViewCell?

Thanks to all i slove this by using below code

     NSIndexPath *indexPath = [panelTableView indexPathForCell:(UITableViewCell *)sender.superview.superview];

NSLog(@"%d",indexPath.row);


Related Topics



Leave a reply



Submit