我正在通過swift3中的故事板在uitableview中實現長按。故事板中只有一個原型單元格。但問題是長按僅在第一個單元中被檢測到。其餘的單元沒有聽到長按手勢。UILongPressGestureRecognizer在表格視圖單元格中使用時出現問題
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let row = indexPath.row
cell.textLabel?.text = "Label"
return cell
}
@IBAction func longPress(_ guesture: UILongPressGestureRecognizer) {
if guesture.state == UIGestureRecognizerState.began {
print("Long Press")
}
}
控制檯中顯示的警告是:
的時間,這是絕對不允許,現在執行。從iOS 9.0開始,它將被放入第一個被加載到的視圖中。
您將手勢附加到哪個視圖? –
uitableview cell –
只需將longpressgesture添加到整個tabelview –