![](/img/trans.png)
[英]Swift 4 - Adding UITapGestureRecognizer to a subview image - the method is not called
[英]Adding UITapGestureRecognizer to subview on tableViewCell
所有人,我正在嘗試使用該代碼向位於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
,我建議另一種方法。 將當前索引行作為標記添加到,然后將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
}
然后在處理程序函數中,您可以通過讀取此標簽來獲取正確的索引路徑
func categoryStackViewTapped(sender: UIStackView) {
let myIndexPath = IndexPath(row: sender.tag, section: 0)
//... other code here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.