我使用部分來加載訊息(viewForFooterInSection)和行來加載特定訊息的回復(如果有)。
以前我在 tableView 上使用長按手勢來檢測 tableView 上的觸摸并使用 tableView.indexPathForRow(at: touchPoint) 回傳 indexPath,但是我還沒有找到類似的方法來獲取長按單元格的 indexPath
任何人都可以幫忙嗎?
uj5u.com熱心網友回復:
當您已經在 tableview 上使用手勢獲得 indexPath 時,我不確定為什么要使用單元格級別的手勢。如果您嘗試從 indexPath 獲取單元格,那么您可以嘗試像
guard let cell = tableView.cellForRow(at: indexPath) else { return }
無論如何要回答您的問題,我們可以通過以下方式從單元格級別獲取 indexPath 。
protocol CustomCellDelegate: AnyObject {
func longPressAction(onCell: CustomCell)
}
class CustomCell: UITableViewCell {
weak var delegate: CustomCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
let lg = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
lg.minimumPressDuration = 0.5
lg.delaysTouchesBegan = true
self.addGestureRecognizer(lg)
}
@objc func longPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizer.State.ended {
return
}
delegate?.longPressAction(onCell: self)
}
}
并在行方法的 tableview 單元格中,分配委托。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell else { return UITableViewCell() }
cell.delegate = self
return cell
}
并確認您的 viewController 中的 CustomCellDelegate 協議。
extension ViewController: CustomCellDelegate {
func longPressAction(onCell: CustomCell) {
guard let indexPath = tableView.indexPath(for: onCell) else { return }
print(indexPath.section, indexPath.row)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/393240.html
上一篇:如何修復VSCode環境中的“發生例外:SSLErrorHTTPSConnectionPool”
下一篇:當我使用xcode使用真實的ios設備進行測驗時,react-native-push-notificationremote不顯示
