如何获取标签或图像视图上的单元格 - UITapGestureRecognizer
How to get cell on label or imageview tap - UITapGestureRecognizer
我的目标是在我点击该单元格的图像视图时执行 segue。但是当我在按钮上使用 addTarget
时不会出现错误。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
cell.imageView.userInteractionEnabled = true
let tapImageView = UITapGestureRecognizer(target: self, action: #selector(HomeFeedViewController.tapImageView(_:)))
cell.imageView.addGestureRecognizer(tapImageView)
return cell as CastleCell
}
func tapImageView(sender: AnyObject) {
let center = sender.center
let point = sender.superview!!.convertPoint(center, toView:self.tableView) //line of error
let indexPath = self.tableView.indexPathForRowAtPoint(point)
let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! CastleCell
performSegueWithIdentifier("SegueName", sender: self)
}
错误行是let point =
...
我得到的错误是:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
但是当我在按钮上使用 addTarget
时,错误没有出现。有什么问题吗?谢谢。
我不太喜欢玩积分和Superview。可以建议的是为 UITapGestureRecognizer 制作一个 class ,如下所示,它可以保存额外的数据。在您的情况下,它将是索引路径
class CustomGesture: UITapGestureRecognizer {
let indexPath:NSIndexPath? = nil
}
然后在您的 didSelect
中,您可以将索引路径添加到新创建的 CustomGesture class,就像:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
cell.imageView.userInteractionEnabled = true
let tapImageView = CustomGesture(target: self, action: #selector(HomeFeedViewController.tapImageView(_:)))
tapImageView.indexPath = indexPath// Add the index path to the gesture itself
cell.imageView.addGestureRecognizer(tapImageView)
return cell as CastleCell
}
现在,由于您已经添加了 indexPath,因此您无需使用超级视图,您可以像这样访问单元格:
func tapImageView(gesture: CustomGesture) {
let indexPath = gesture.indexPath!
let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! CastleCell
performSegueWithIdentifier("SegueName", sender: self)
}
我的目标是在我点击该单元格的图像视图时执行 segue。但是当我在按钮上使用 addTarget
时不会出现错误。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
cell.imageView.userInteractionEnabled = true
let tapImageView = UITapGestureRecognizer(target: self, action: #selector(HomeFeedViewController.tapImageView(_:)))
cell.imageView.addGestureRecognizer(tapImageView)
return cell as CastleCell
}
func tapImageView(sender: AnyObject) {
let center = sender.center
let point = sender.superview!!.convertPoint(center, toView:self.tableView) //line of error
let indexPath = self.tableView.indexPathForRowAtPoint(point)
let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! CastleCell
performSegueWithIdentifier("SegueName", sender: self)
}
错误行是let point =
...
我得到的错误是:
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
但是当我在按钮上使用 addTarget
时,错误没有出现。有什么问题吗?谢谢。
我不太喜欢玩积分和Superview。可以建议的是为 UITapGestureRecognizer 制作一个 class ,如下所示,它可以保存额外的数据。在您的情况下,它将是索引路径
class CustomGesture: UITapGestureRecognizer {
let indexPath:NSIndexPath? = nil
}
然后在您的 didSelect
中,您可以将索引路径添加到新创建的 CustomGesture class,就像:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
cell.imageView.userInteractionEnabled = true
let tapImageView = CustomGesture(target: self, action: #selector(HomeFeedViewController.tapImageView(_:)))
tapImageView.indexPath = indexPath// Add the index path to the gesture itself
cell.imageView.addGestureRecognizer(tapImageView)
return cell as CastleCell
}
现在,由于您已经添加了 indexPath,因此您无需使用超级视图,您可以像这样访问单元格:
func tapImageView(gesture: CustomGesture) {
let indexPath = gesture.indexPath!
let cell = self.tableView.cellForRowAtIndexPath(indexPath!) as! CastleCell
performSegueWithIdentifier("SegueName", sender: self)
}