简体   繁体   中英

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. The tableView is being displayed using a custom UIView and the UITableViewCell are also custom. 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. This feature kinda of works but when I press for more then 3 seconds the UITableViewCell resets to this

UITableViewCell Error ScreenShot

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:

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:

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. 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. A tap gesture recognizer is not needed as this is a natively provided function.

UITableViewDelegate Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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