Dia*_*Dia 1 label uitableview ios swift
我有一个自定义UITableViewCell,其中有一个Label,需要根据特定条件使其可点击。TapGestureRecognizer所以我在上面加了一个。我已经使用了协议和代表来实现同样的目的。我想传递一个参数并在单击 this 时执行转场UILabel。我能够执行此操作,但无法检测到它是哪个单元格的标签。
我对此很陌生,并且已经被困了几个小时。任何帮助,将不胜感激。还请告诉我是否有另一种更简单的方法可以达到相同的结果。
如果它是一个按钮,我可以添加一个目标,但我现在真的陷入困境。
正如 @KrishnaCA 在他们的回答中所说,如果您希望整个标签可点击,那么最好使用按钮。如果您需要它看起来与按钮不同,您还可以在标签顶部放置一个透明按钮(没有图像的自定义样式)。
无论您使用按钮还是标签+点击手势识别器,您仍然需要弄清楚用户点击了哪个indexPath。您可以按照哈米德的建议在按钮/标签中放置一个标签,但这很脆弱。我更喜欢不同的方式。我已经编写了 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
}
}
Run Code Online (Sandbox Code Playgroud)
如果您将该扩展添加到您的项目中,您可以使用如下代码。
手势识别器:
@objc func alertClicked(sender: UIGestureRecognizer){
let view = sender.view
let indexPath = tableView.indexPathForView(view)
//Do whatever you need to do with the indexPath
}
Run Code Online (Sandbox Code Playgroud)
或者,对于按钮操作:
@objc func buttonClicked(sender: UIButton) {
let indexPath = tableView.indexPathForView(sender)
//Do whatever you need to do with the indexPath
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3347 次 |
| 最近记录: |