大家,我正在尝试使用该代码将手势识别器添加到位于TableViewCell上的StackView,但它不起作用:
@IBOutlet var categoryStackView: UIStackView! {
didSet {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(categoryStackViewTapped))
categoryStackView.addGestureRecognizer(tapGesture)
}
}
它不起作用,我检查了 StackView,它启用了用户交互
@objc func categoryStackViewTapped(sender: UITapGestureRecognizer) {
print("Here we are")
}
如果您的tableView
有多个stackView
,我建议另一种方法。将当前索引行作为标记添加到 ,然后将单击操作添加到按钮处理程序函数。
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
}
然后在您的处理程序函数中,您可以通过读取此标记来获取正确的索引路径
func categoryStackViewTapped(sender: UIStackView) {
let myIndexPath = IndexPath(row: sender.tag, section: 0)
//... other code here
}