Swift 如何使用UILongPressGestureRecognitor从tableview单元格按钮传递数据?
我的手机里有一个按钮,如果用户按住一段时间,就会触发弹出窗口。我用长按按钮传递单元格数据时遇到问题 下面是我如何提交和通过定期点击数据Swift 如何使用UILongPressGestureRecognitor从tableview单元格按钮传递数据?,swift,tableview,Swift,Tableview,我的手机里有一个按钮,如果用户按住一段时间,就会触发弹出窗口。我用长按按钮传递单元格数据时遇到问题 下面是我如何提交和通过定期点击数据 cell.addButton.tag = (indexPath as NSIndexPath).row cell.addButton.addTarget(self, action: #selector(Dumps.addAction(_:)), for: UIControl.Event.touchUpInside) 以上方法很好 下面是我如何提交长按按钮
cell.addButton.tag = (indexPath as NSIndexPath).row
cell.addButton.addTarget(self, action: #selector(Dumps.addAction(_:)), for: UIControl.Event.touchUpInside)
以上方法很好
下面是我如何提交长按按钮的手势。我想这已经超过零了
cell.deleteButton.tag = (indexPath as NSIndexPath).row
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(Dumps.deleteAction(_:)))
cell.deleteButton.addGestureRecognizer(longGesture)
如何通过此方法传递数据?您应该使用
ui按钮的标记,而不是像上面所做的那样使用ui长按gesture识别器
func deleteAction(_ sender: UILongPressGestureRecognizer) {
guard let tag = (sender.view as? UIButton)?.tag else { return }
let cell = tableView.cellForRow(at: IndexPath(row: tag, section: 0)) as? DumpsCell01
cell?.codeLabel.backgroundColor = .red
}
注意:我还避免了强制展开,因为在整个项目中您也应该这样做
@objc func deleteAction(_ sender: UIGestureRecognizer){
let tag = (sender as AnyObject).tag
let cell = tableView.cellForRow(at: IndexPath.init(row: tag!, section: 0)) as! DumpsCell01
cell.codeLabel.backgroundColor = UIColor.red }
func deleteAction(_ sender: UILongPressGestureRecognizer) {
guard let tag = (sender.view as? UIButton)?.tag else { return }
let cell = tableView.cellForRow(at: IndexPath(row: tag, section: 0)) as? DumpsCell01
cell?.codeLabel.backgroundColor = .red
}