【问题标题】:UITableView's didSelectRow method not working [duplicate]UITableView 的 didSelectRow 方法不起作用[重复]
【发布时间】:2019-08-30 03:14:59
【问题描述】:

我没有收到触摸。它在 iPhone 屏幕上显示选择,但不执行方法didSelectRow

    usersTV.register(UserCell.self, forCellReuseIdentifier: "userCell")
    usersTV.dataSource = self
    usersTV.delegate = self
    usersTV.backgroundColor = .clear
    usersTV.separatorColor = .white
    usersTV.translatesAutoresizingMaskIntoConstraints = false
    usersTV.isHidden = true
    usersTV.tableFooterView = UIView()
    usersTV.isUserInteractionEnabled = true
    usersTV.isEditing = false
    usersTV.allowsSelection = true
    usersTV.allowsSelectionDuringEditing = false
    usersTV.allowsMultipleSelection = false
    usersTV.allowsMultipleSelectionDuringEditing = false
    usersTV.delaysContentTouches = false

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

一切都是以编程方式编写的。 这些单元格是自定义的,带有 3 个标签并添加了约束。

可能是什么问题?

更新:

class UserCell : UITableViewCell{
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        let viewsDict = ["userName":userName,"field1":field1,"field2":field2]

    contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[userName(120)]-8-[field1]-8-[field2(120)]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
    contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[userName]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
    contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[field1]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))
    contentView.superview?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[field2]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDict))

更新: didSelectRow 方法仅在您用两根手指点击时才有效...

【问题讨论】:

    标签: uitableview swift4 didselectrowatindexpath


    【解决方案1】:

    TableViewCell is not clickable with one finger tap, but it is with two fingers

    这个问题的答案帮助了我。我在主视图上设置了 GestureRecognizer。当我删除它时,问题就解决了。

    【讨论】:

      【解决方案2】:

      我认为这段代码会对你有所帮助。如果没有,可能是标签约束有问题。

      @IBOutlet weak var UserTV: UITableView!
      override func viewDidLoad() {
          super.viewDidLoad()
          UserTV.register(UserCell.self, forCellReuseIdentifier: "userCell")
          UserTV.dataSource = self
          UserTV.delegate = self
      
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return 10
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let vc :UserCell = self.UserTV.dequeueReusableCell(withIdentifier: "userCell") as! UserCell
          vc.textLabel?.text = "Index: \(indexPath.row)"
          vc.backgroundColor = UIColor.red
          return vc
      }
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          print("Slected \(indexPath.row)")
      }
      

      【讨论】:

      • 约束如何与单元格上的触摸相关联?他们怎么会出问题?
      • 我更新了问题。看看!
      【解决方案3】:

      首先,这是我对您的代码的评论:

      • 这项简单的任务不需要太复杂。
      • 别忘了正确设置委托和数据源。
      • 无需使用Visual Format方式设置约束,使用Anchors。

      我给你的建议:

      • 始终尝试使用 UICollectionView 而不是 UITableView。

      类 SampleTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

      // MARK: - Variables
      lazy private var listTableView: UITableView = {
          let tableView = UITableView()
          tableView.translatesAutoresizingMaskIntoConstraints = false
          tableView.delegate = self
          tableView.dataSource = self
          return tableView
      }()
      
      // MARK: - View LifeCycle
      override func viewDidLoad() {
          super.viewDidLoad()
          initialSetup()
      }
      
      // MARK: - Private Methods
      private func initialSetup() {
          view.backgroundColor = .white
      
          view.addSubview(listTableView)
          listTableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
          listTableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
          listTableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
          listTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
          listTableView.register(UITableViewCell.self, forCellReuseIdentifier: "listCell")
      }
      
      // MARK: - TableView Methods
      func numberOfSections(in tableView: UITableView) -> Int {
          return 1
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return 20
      }
      
      func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 50
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "listCell", for: indexPath)
          cell.textLabel?.text = "Table Row - \(indexPath.row)"
          return cell
      }
      
      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          print(#function)
      }
      

      【讨论】:

      • 我必须使用我的代码,因为我在 GameScene 中显示表格视图。约束在 CustomClassCell 中设置: UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { constraints } } 我对 tableView 约束没有问题。显然问题出在单元格的 contentView 的约束中。
      • 你能分享一下你想要如何显示你的单元格的图片吗?
      • H:|-8-[userName(120)]-8-[field1]-8-[field2(120)]-8-| 前导,用户名 120 宽度,8 个空格,字段 1, 8 个空格,字段 2 120 宽度,空格 8,尾随
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多