Swift UiTableViewCell长按后重置

时间:2017-03-08 02:09:04

标签: ios swift uitableview uiview uitapgesturerecognizer

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

UITableViewCell Error ScreenShot

我不知道导致此错误的原因。任何帮助将不胜感激。

这是我的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
    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

UITableViewDelegate Documentation