ios - didSelectRowAt 未触发(Swift 5)

标签 ios swift uitableview

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

我的问题:我的 tableView 单元格的 react 不一致。有时它们会响应 didSelectRowAt,有时则不会。那么为什么有时我可以点击某个单元格并让它做出响应,而有时却不能呢?它甚至不是每次都是同一个单元格,如果我关闭并重新调用该表格,它可能是不同的单元格或相同的单元格。

我的 configureCell 函数可以正常工作。 (我知道这不是问题,因为我尝试过使用和不使用该功能进行设置。)

我尝试过的

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

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

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

2) 我已四次检查是否在单元格级别启用了用户交互启用,并在表格级别启用了“单选”。

3) 我什至尝试删除 didSelectRowAt 函数上的所有其他代码(除了 print() 函数),只是为了确保它不是我的代码,有时它会叫,有时却不会,毫无规律或理由。

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

这是我的代码:

作为引用,RoundScoreType 是一个具有 2 个变量的类:var name: Stringvar isSelected: BoolRoundScoreMenu 是一个包含一些函数的类,但基本上是 2 个 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 选择选项从“单选”翻转为“多选”,突然之间,它起作用了。仍然可能是我的计算机编译它的问题,但这是我改变的最后一件事,它突然开始工作。

感谢您的帮助!

关于ios - didSelectRowAt 未触发(Swift 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59348937/

相关文章:

ios - 如何覆盖 .swift 中的 oc 类别方法

ios - 我应该使用 UIDocument 来处理已保存到磁盘的 PDF 文件吗?

ios - 访问自定义推送通知负载

ios - 过渡不适用于 SwiftUI 中的路由器

ios - 实例变量总是返回零

objective-c - iOS - 防止用户将可排序的 UITableViewCell 拖动到顶行

ios - 如何根据点击的单元格更改 View Controller 的数据

ios - 将 NSArray 传递到 tableview swift 3.0

ios - 如何删除 TableView ios 的索引路径中的重复项,

swift - 为什么我不能在 Range 扩展中循环 self?