繁体   English   中英

Swift UiTableViewCell长按后重置

[英]Swift UiTableViewCell Resets after long press

在我的主视图控制器中,我有一个按钮,弹出一个带有两个按钮和一个tableView的对话框。 tableView使用自定义UIView显示,UITableViewCell也是自定义的。 UITableViewCell包含一个自定义复选框和一个UILabel。我正在尝试向tableView添加一个轻击手势,这样当我点击一行时它会标记复选框。 这个功能有点可行,但是当我按下3秒以上时,UITableViewCell会重置为此

UITableViewCell错误Scre​​enShot

我不知道是什么造成了这个错误。 任何帮助,将不胜感激。

这是我的ViewController中的代码,它打开了弹出对话框:

func locationButtonPressed(sender: UIBarButtonItem) {
    // Create a custom view controller
    let vc = RadiusViewController(nibName: "RadiusViewController", bundle: nil)
    // Create the dialog
    let popup = PopupDialog(viewController: vc, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)

    // create first button
    let cancelButton = CancelButton(title: "Cancel", height: 60, dismissOnTap: true) {
        print("Popup Canceled")
    }

    // create second button
    let okButton = DefaultButton(title: "Ok", height: 60, dismissOnTap: true) {
        print("Ok button pressed")
    }

    // add buttons to dialog
    popup.addButtons([cancelButton, okButton])

    // present dialog
    self.present(popup, animated: true, completion: nil)

    print("location button pressed")
}

使用tableView在我的自定义UIView中点击手势功能:

override func viewDidLoad() {
    super.viewDidLoad()

    ...code

    let tap = UITapGestureRecognizer(target: self, action: #selector(tableTapped))
    self.tableView.addGestureRecognizer(tap)
}

func tableTapped(tap:UITapGestureRecognizer) {
    let location = tap.location(in: self.tableView)
    let path = self.tableView.indexPathForRow(at: location)
    if let indexPathForRow = path {
        self.tableView(self.tableView, didSelectRowAt: indexPathForRow)
        print("Tapped on the table")
    } else {
        // handle tap on empty space below existing rows however you want
    }
}

我想你可能需要在tableviewcell上添加点击手势,而不是tableview。

您只需要让ViewController实现UITableViewDelegate。 didSelectRowAtIndexPath有一个回调方法,您可以在其中设置逻辑以访问单元格的属性,例如自定义复选框。 不需要轻击手势识别器,因为这是本机提供的功能。

UITableViewDelegate文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM