【问题标题】:Swift UiTableViewCell Resets after long press长按后Swift UiTableViewCell重置
【发布时间】:2023-03-23 20:00:01
【问题描述】:

在我的主视图控制器中,我有一个按钮,它会弹出一个带有两个按钮和一个 tableView 的对话框。 tableView 正在使用自定义 UIView 显示,并且 UITableViewCell 也是自定义的。 UITableViewCell 由一个自定义复选框和一个 UILabel 组成。我正在尝试向 tableView 添加一个点击手势,以便当我单击一行时它会标记该复选框。这个功能有点用,但是当我按下超过 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
    }
}

【问题讨论】:

    标签: ios swift uitableview uiview uitapgesturerecognizer


    【解决方案1】:

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

    【讨论】:

    • 我该怎么做呢?
    【解决方案2】:

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

    UITableViewDelegate Documentation

    【讨论】:

      猜你喜欢
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多