简体   繁体   English

didSelectRowAt 未触发(Swift 5)

[英]didSelectRowAt not triggering (Swift 5)

I've tried looking up other responses, and none of them seem to help.我试过查找其他回复,但似乎都没有帮助。 Also, this is in Swift 5 and many of the other responses I see are for older versions.此外,这是在 Swift 5 中,我看到的许多其他响应都是针对旧版本的。

My issue: My tableView cells don't react consistently.我的问题:我的 tableView 单元格的反应不一致。 Sometimes they respond to didSelectRowAt , sometimes they don't.有时他们回应didSelectRowAt ,有时他们不回应。 So why is it that sometimes I can tap a cell and get it to respond, and other times I can't?那么为什么有时我可以点击一个单元格并让它响应,而其他时候我不能? It's not even the same cell every time, and if I dismiss and recall the table, it may be different cells or the same ones.甚至每次都不是同一个单元格,如果我关闭并回忆表格,它可能是不同的单元格或相同的单元格。

My configureCell function works without issue.我的configureCell功能可以正常工作。 (I know it's not the problem, because I've tried setting things both with and without the function.) (我知道这不是问题,因为我已经尝试在有和没有该功能的情况下进行设置。)

What I've tried我试过的

1) I've tried both setting and not setting: 1)我试过设置和不设置:

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

And I've tried setting the allowsSelection or isUserInteractionEnabled both at the tableView level and the tableViewCell level.我已经尝试在tableView级别和tableViewCell级别设置allowsSelectionisUserInteractionEnabled

2) I've quadruple checked that User Interaction Enabled is turned on at the cell level, and "Single Selection" is turned on at the table level. 2)我已经四次检查了在单元格级别User Interaction EnabledUser Interaction Enabled ,并在表级别User Interaction Enabled了“单选”。

3) I've even tried removing all other code on the didSelectRowAt function except for a print() function just to make sure it wasn't my code, and sometimes it would call, other times it wouldn't, with no rhyme or reason. 3) 我什至尝试删除didSelectRowAt函数上的所有其他代码,除了print()函数只是为了确保它不是我的代码,有时它会调用,有时不会,没有押韵或原因。

Is it possible that it's my computer, and not my code that's the problem?有没有可能是我的电脑,而不是我的代码出了问题? I'm running macOS Catalina 10.15.2 with Xcode 11.3 .我正在使用Xcode 11.3运行macOS Catalina 10.15.2

Here's my code:这是我的代码:

For reference, the RoundScoreType is a class with 2 variables: var name: String and var isSelected: Bool .作为参考, RoundScoreType是一个具有 2 个变量的类: var name: Stringvar isSelected: Bool RoundScoreMenu is a class with some functions in it, but basically is a place holder for 2 arrays of RoundScoreType with 5 entries each and some functions that I'm not calling yet, other than the tallyScore function, which hasn't been a problem. RoundScoreMenu是一个包含一些函数的类,但基本上是 2 个RoundScoreType数组的RoundScoreType ,每个数组有 5 个条目和一些我还没有调用的函数,除了tallyScore函数,这不是问题。 (Once again, tried both with and without the function, and no appreciable difference) (再一次,尝试了有和没有功能,并没有明显的区别)

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()
    }

}

I have no idea why it worked or made a difference, but I flipped the tableView selection option from "Single Selection" to "Multiple Selection" and all of a sudden, it worked.我不知道它为什么有效或tableView ,但我将tableView选择选项从“单选”翻转到“多选”,突然之间,它起作用了。 It could still be that it was a problem with my computer compiling it, but that is the last thing I changed and it suddenly started working.仍然可能是我的计算机编译它有问题,但这是我更改的最后一件事,它突然开始工作。

Thanks for the help!谢谢您的帮助!

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

相关问题 NotificationCenter在iOS Swift 3中的didSelectRowAt中不起作用 - NotificationCenter not working in didSelectRowAt in iOS Swift 3 快速地,在didSelectRowAt之前调用prepare - In swift, prepare is invoked before didSelectRowAt Swift 中的 didSelectRowAt Function 上的 Switch 语句 - Switch Statement on didSelectRowAt Function in Swift UITableView和UICollectionView上的Swift didSelectRowAt不起作用 - Swift didSelectRowAt on UITableView and UICollectionView not works Xcode 8 / Swift 3:didSelectRowAt vs didSelectRowAtIndexPath - Xcode 8 / Swift 3: didSelectRowAt vs didSelectRowAtIndexPath 斯威夫特 5 | didSelectRowAt 同时选择两个单元格 - Swift 5 | didSelectRowAt is selecting two cells at the same time 在didselectRowAt(Swift 3)时如何添加子行 - how can i add subrow when didselectRowAt ( Swift 3) didSelectRowAt Swift 3即使在设置委托和dataSource后也不调用 - didSelectRowAt Swift 3 Not Called even after delegate and dataSource are set 呈现警报控制器后,TableView(didSelectRowAt IndexPath)中的快速继续流 - Swift Continue Flow in TableView(didSelectRowAt IndexPath) after presenting Alert Controller iOS Swift 4-在表视图didShowRowAt上显示Toast(或活动指示器) - iOS Swift 4 - Show Toast (or Activity Indicator) on tableview didSelectRowAt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM