繁体   English   中英

didSelectRowAt 未触发(Swift 5)

[英]didSelectRowAt not triggering (Swift 5)

我试过查找其他回复,但似乎都没有帮助。 此外,这是在 Swift 5 中,我看到的许多其他响应都是针对旧版本的。

我的问题:我的 tableView 单元格的反应不一致。 有时他们回应didSelectRowAt ,有时他们不回应。 那么为什么有时我可以点击一个单元格并让它响应,而其他时候我不能? 甚至每次都不是同一个单元格,如果我关闭并回忆表格,它可能是不同的单元格或相同的单元格。

我的configureCell功能可以正常工作。 (我知道这不是问题,因为我已经尝试在有和没有该功能的情况下进行设置。)

我试过的

1)我试过设置和不设置:

  • tableView.delegate = self
  • tableView.dataSource = self
  • tableView.allowsSelection = true
  • tableView.isUserInteractionEnabled = true

我已经尝试在tableView级别和tableViewCell级别设置allowsSelectionisUserInteractionEnabled

2)我已经四次检查了在单元格级别User Interaction EnabledUser Interaction Enabled ,并在表级别User Interaction Enabled了“单选”。

3) 我什至尝试删除didSelectRowAt函数上的所有其他代码,除了print()函数只是为了确保它不是我的代码,有时它会调用,有时不会,没有押韵或原因。

有没有可能是我的电脑,而不是我的代码出了问题? 我正在使用Xcode 11.3运行macOS Catalina 10.15.2

这是我的代码:

作为参考, RoundScoreType是一个具有 2 个变量的类: var name: Stringvar isSelected: Bool RoundScoreMenu是一个包含一些函数的类,但基本上是 2 个RoundScoreType数组的RoundScoreType ,每个数组有 5 个条目和一些我还没有调用的函数,除了tallyScore函数,这不是问题。 (再一次,尝试了有和没有功能,并没有明显的区别)

protocol ScoreTableDelegate {
    func scoreTableDelegate(team1Score: Int, team2Score: Int, round: Int)
}

class ScoreTableTVC: UITableViewController {

    var team1Name = "Team 1"
    var team2Name = "Team 2"
    var team1List = RoundScoreMenu().team1List
    var team2List = RoundScoreMenu().team2List

    var team1RoundScore: Int = 0
    var team2RoundScore: Int = 0

    var round: Int?
    var delegate: ScoreTableDelegate?

    func configureCell(cell: UITableViewCell, item: RoundScoreType){
        cell.textLabel!.text = item.name
        if item.isSelected {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }

    }



    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.allowsSelection = true
        tableView.isUserInteractionEnabled = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        delegate?.scoreTableDelegate(team1Score: team1RoundScore, team2Score: team2RoundScore, round: round!)
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 5
    }

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return team1Name + ": " + String(team1RoundScore.description)
        } else {
            return team2Name + ": " + String(team2RoundScore.description)
        }
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ScoreCell", for: indexPath)
        if indexPath.section == 0 {
            configureCell(cell: cell, item: team1List[indexPath.row])
        } else {
            configureCell(cell: cell, item: team2List[indexPath.row])
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.isSelected = true
        print("Section: \(indexPath.section), row: \(indexPath.row)")
        if indexPath.section == 0 {
            team1List[indexPath.row].toggleSelection()
        } else {
            team2List[indexPath.row].toggleSelection()
        }
        tallyScore(indexPath.section)
        tableView.reloadData()
    }

}

我不知道它为什么有效或tableView ,但我将tableView选择选项从“单选”翻转到“多选”,突然之间,它起作用了。 仍然可能是我的计算机编译它有问题,但这是我更改的最后一件事,它突然开始工作。

谢谢您的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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