[英]How to get the indexpath.row when a UIImageView in a cell is tapped?
swift: how to get the indexpath.row when a button in a cell is tapped? 迅捷:轻按单元格中的按钮时如何获取indexpath.row?
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. 我只是想知道是否有可能通过点击
UIImageView
而不是按钮来实现。 I tried the exact answer with a UIImageView
instead of a UIButton
and I got this error 我尝试使用
UIImageView
而不是UIButton
的确切答案,但出现此错误
"fatal error: unexpectedly found nil while unwrapping an Optional value".
“致命错误:在展开可选值时意外发现nil”。
cell.imagePosted
is a UIImageView
cell.imagePosted
是一个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)
}
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 将
UITapGestureRecognizer
添加到UIImageView
您可以将indexpath.row
存储在UIImageView
tag属性中,并在UITapGestureRecognizer
事件上访问该标签
for example (Objective-C) : 例如(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
并获取
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);
}
You can create a protocol with a method like imageViewInCellTapped(cell:YourCellType)
and a delegate property in the cell. 您可以使用诸如
imageViewInCellTapped(cell:YourCellType)
类的方法以及该单元格中的委托属性来创建协议。 In cellForRowAtIndexPath
set the controller the delegate of each cell and implement the method from the protocol. 在
cellForRowAtIndexPath
为控制器设置每个单元的委托,并从协议中实现方法。 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. 当您的单元格中发生某些事情(例如,点击按钮或图像)时,可以调用
delegate?.imageViewInCellTapped(self)
,其中self是单元格,然后在实现此方法的控制器中,使用tableView的indexPathForCell
方法获取索引。
Here is Madan's code in swift if anybody is interested: 如果有人感兴趣,以下是Madan的代码:
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)
}
Hope this code helps to get indexpath for tapped cell swift 3: 希望这段代码有助于获取tapped cell swift 3的indexpath:
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)
}
}*/
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.