Reputation: 13
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)
}
Upvotes: 0
Views: 1685
Reputation: 3811
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)
}
}*/
}
Upvotes: 0
Reputation: 13
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)
}
Upvotes: 0
Reputation: 668
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);
}
Upvotes: 1
Reputation: 490
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.
Upvotes: 0