[英]Adding UITapGestureRecognizer to subview on tableViewCell
everyone, I am trying to add gesture recognizer to StackView which located on TableViewCell with that code and it doesn't work: 所有人,我正在尝试使用该代码向位于TableViewCell上的StackView添加手势识别器,但它不起作用:
@IBOutlet var categoryStackView: UIStackView! {
didSet {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(categoryStackViewTapped))
categoryStackView.addGestureRecognizer(tapGesture)
}
}
And it doesn't work, I checked StackView and it is enable for user interactive 而且它不起作用,我检查了StackView并启用了用户交互功能
@objc func categoryStackViewTapped(sender: UITapGestureRecognizer) {
print("Here we are")
}
I am suggesting another approach if you have multiple stackView
in your tableView
. 如果您在
tableView
有多个stackView
,我建议另一种方法。 Add the current index row as tag to your , and then add click action to your button handler function. 将当前索引行作为标记添加到,然后将click动作添加到按钮处理函数。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
cell.stackView.tag = indexPath.row
cell.stackView.addTarget(self, action:#selector(categoryStackViewTapped(sender:)), for: .touchUpInside)
return cell
}
Then in your handler function, you can get the correct index path by reading this tag 然后在处理程序函数中,您可以通过读取此标签来获取正确的索引路径
func categoryStackViewTapped(sender: UIStackView) {
let myIndexPath = IndexPath(row: sender.tag, section: 0)
//... other code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.