简体   繁体   English

Swift UiTableViewCell长按后重置

[英]Swift UiTableViewCell Resets after long press

in my main View Controller I have a button that popups a dialog with two buttons and a tableView. 在我的主视图控制器中,我有一个按钮,弹出一个带有两个按钮和一个tableView的对话框。 The tableView is being displayed using a custom UIView and the UITableViewCell are also custom. tableView使用自定义UIView显示,UITableViewCell也是自定义的。 The UITableViewCell consists of a custom checkbox and a UILabel.I'm trying to add a tap gesture to the tableView so that when I click on a row it marks the checkbox. UITableViewCell包含一个自定义复选框和一个UILabel。我正在尝试向tableView添加一个轻击手势,这样当我点击一行时它会标记复选框。 This feature kinda of works but when I press for more then 3 seconds the UITableViewCell resets to this 这个功能有点可行,但是当我按下3秒以上时,UITableViewCell会重置为此

UITableViewCell Error ScreenShot UITableViewCell错误Scre​​enShot

I don't know whats causing this error. 我不知道是什么造成了这个错误。 Any help would be appreciated. 任何帮助,将不胜感激。

Here is my code in my ViewController that opens the popup dialog: 这是我的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")
}

Tap Gesture Function in my Custom UIView with the tableView: 使用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。

You just need to have your ViewController implement the UITableViewDelegate. 您只需要让ViewController实现UITableViewDelegate。 There is a callback method for didSelectRowAtIndexPath and in there you can set logic to access your cell's properties, such as the custom checkbox. didSelectRowAtIndexPath有一个回调方法,您可以在其中设置逻辑以访问单元格的属性,例如自定义复选框。 A tap gesture recognizer is not needed as this is a natively provided function. 不需要轻击手势识别器,因为这是本机提供的功能。

UITableViewDelegate Documentation UITableViewDelegate文档

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

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