Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用UILongPressGestureRecognitor从tableview单元格按钮传递数据?_Swift_Tableview - Fatal编程技术网

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
}