我有一个自定义的 UITableViewCell
,上面有一个 Label
,需要根据特定条件使其可点击。所以我在上面添加了一个 TapGestureRecognizer
。我为此使用了协议(protocol)和代表。我想在单击此 UILabel
时传递一个参数并执行 segue。我能够执行此 segue,但无法检测到它是哪个单元格的标签。
我是新手,已经卡了几个小时了。任何帮助,将不胜感激。还请告诉我是否有另一种更简单的方法来获得相同的结果。
如果它是一个按钮,我可以添加一个目标,但我现在真的卡住了。
最佳答案
正如@KrishnaCA 在他们的回答中所说,如果您希望整个标签都可以点击,那么使用按钮可能会更好。如果您需要它看起来与按钮不同,您还可以在标签顶部放置一个透明按钮(没有图像的自定义样式)。
无论您使用按钮还是标签 + 点击手势识别器,您仍然需要弄清楚用户点击了哪个 indexPath。您可以按照哈米德的建议在按钮/标签中放置一个标签,但这很脆弱。我更喜欢不同的方式。我已经为 UITableView 编写了一个扩展,它可以让你找出哪个单元格拥有给定的 View :
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
}
关于ios - 如何识别标签点击自定义 UITableViewCell - Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41522441/