【问题标题】:didSelectRowAt is not getting calleddidSelectRowAt 没有被调用
【发布时间】:2019-06-14 03:00:21
【问题描述】:

我有一个 tableView,我设置了委托和数据源,但是当点击一个单元格时,没有调用 didSelectRowAt 函数。 这是我的代码:

private let reuseIdentifier = "DropdownCell"

extension ReportVC: UITableViewDelegate, UITableViewDataSource {


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return showMenu ? companies.count : .zero //If show menu is true the tableView needs to be shown, so we are returning the number of companies in the array, if the show menu is flase the tableView does NOT needs to be shown, so we are returning zero.
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! CompanyCell
    cell.companyNameLabel.text = companies[indexPath.row]

    let bgColorView = UIView()
    bgColorView.backgroundColor = UIColor.appBlueAccent

    cell.selectedBackgroundView = bgColorView

    return cell
}

//TODO: add arrow to indicate the dropdown list, adjust the select company label to the side of the screen
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let button = UIButton(type: .system)
    button.setTitle("Select Company", for: .normal)
    button.setTitleColor(UIColor.appWhite, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.addTarget(self, action: #selector(handleDropDown), for: .touchUpInside)
    button.backgroundColor = UIColor.appBlueAccent

    return button
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
}

func configureTableView(){
    tableView = UITableView()
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.isScrollEnabled = false
    tableView.rowHeight = 50
    tableView.separatorStyle = .none
    tableView.backgroundColor = UIColor.appWhite
    tableView.delegate = self
    tableView.dataSource = self

    tableView.register(CompanyCell.self, forCellReuseIdentifier: reuseIdentifier)

    view.addSubview(tableView)
    NSLayoutConstraint.activate([
        tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0),
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
        tableView.heightAnchor.constraint(equalToConstant: 250),

        ])
}


@objc func handleDropDown(){

    showMenu = !showMenu
    var indexPaths = [IndexPath]()

    for i in 0..<companies.count{
        let indexPath = IndexPath(row: i, section: 0)
        indexPaths.append(indexPath)
    }

    if showMenu {
        tableView.insertRows(at: indexPaths, with: .automatic)
    }else{
        tableView.deleteRows(at: indexPaths, with: .automatic)
    }

}
}

这是我的 ReportVC 的扩展,在报告 VC 中我调用 configureTableView 并设置 showMenu 和 tableView 变量。 我尝试打印 indexPath 或者实际上只是打印一个字符串以查看是否调用了 didSelectRowAt,但没有任何反应。

【问题讨论】:

  • 这可能不相关,但您的handleDropDown 是错误的。你需要在这里打电话给performBatchUpdates
  • 您在使用 Storyboard 吗?也许你有一个干扰didSelectRowAt 的segue。您应该只使用其中之一。
  • @Koen 谢谢,但我没有使用情节提要。
  • 您能否通过从单一视图应用程序模板开始将其简化为最小可重现的示例项目?如果有,请发布。

标签: swift


【解决方案1】:

我认为水龙头正在吞噬我单元格的内容。我要做的是使用 Debug View Hierarchy 来查看布局。

【讨论】:

  • 谢谢,我尝试删除单元格中的 UILabel,但仍然无法使用。
猜你喜欢
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多