如何识别点击自定义 UITableViewCell 的标签 - Swift 3

how to recognize label click on custom UITableViewCell - Swift 3

我有一个自定义 UITableViewCell,上面有一个 Label,需要根据特定条件使其可点击。所以我在上面添加了一个TapGestureRecognizer。我为此使用了协议和代表。我想在单击此 UILabel 时传递一个参数并执行 segue。我能够执行此 segue,但无法检测到它是哪个单元格的标签。

我是新手,卡了几个小时。任何帮助,将不胜感激。还请告诉我是否有另一种更简单的方法来获得相同的结果。

如果它是一个按钮,我可以添加一个目标,但我现在真的卡住了。

给每个单元格一个唯一的标签,然后:

 self.tag = x // x is a unique number
 label.isUserInteractionEnabled = true

let tapGesture = UITapGestureRecognizer(target: self, action:#selector(self.alertClicked(sender:)))
self.label.addGestureRecognizer(tapGesture)
tapGesture.delegate = self

并将此函数添加到您的 CustomCell Class:

func alertClicked(sender: UITapGestureRecognizer){
    print(self.tag)// prints x

}

正如@KrishnaCA 在他们的回答中所说,如果您希望整个标签都可以点击,那么使用按钮可能会更好。如果您需要它看起来与按钮不同,您还可以在标签顶部放置一个透明按钮(没有图像的自定义样式)。

无论您使用按钮还是标签 + 点击手势识别器,您仍然需要弄清楚用户点击了哪个 indexPath。您可以按照哈米德的建议在 button/label 中放置一个标签,但这很脆弱。我更喜欢不同的方式。我已经为 UITableView 编写了一个扩展,它可以让你找出哪个单元格拥有给定的视图:

public extension UITableView {

  /// This method returns the indexPath of the cell that contains the specified view
  /// - parameter view: The view to find.
  /// - returns: The indexPath of the cell containing the view, or nil if it can't be found

  func indexPathForView(_ view: UIView) -> IndexPath? {
    let origin = view.bounds.origin
    let viewOrigin = self.convert(origin, from: view)
    let indexPath = self.indexPathForRow(at: viewOrigin)
    return indexPath
  }
}

如果将该扩展添加到您的项目中,您可以使用如下代码。

手势识别器:

@objc func alertClicked(sender: UIGestureRecognizer){
  let view = sender.view
  let indexPath = tableView.indexPathForView(view)
  //Do whatever you need to do with the indexPath
}

或者,对于按钮操作:

@objc func buttonClicked(sender: UIButton) {
  let indexPath = tableView.indexPathForView(sender)
  //Do whatever you need to do with the indexPath 
} 

我个人更喜欢子class TableViewCell 并使用委托方法将点击发送回 TableView。您将需要在 TableViewCell 中提供并保存 indexPath,然后使用委托方法将其传回,这在技术上破坏了 MVC,但就个人而言,这种方式的代码更加清晰。如果您对此感兴趣,我可以 post 编写代码。