首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >didSelectRowAt未触发(Swift 5)

didSelectRowAt未触发(Swift 5)
EN

Stack Overflow用户
提问于 2019-12-15 23:19:58
回答 1查看 221关注 0票数 0

我试过查找其他的回复,但似乎都没有帮助。此外,这是在Swift 5和我看到的许多其他反应是为较早的版本。

我的问题是:我的tableView细胞没有一致的反应。有时他们会响应didSelectRowAt**,,有时他们不会。那么,为什么_有时是_,我可以点击一个单元,让它响应,而另一些时候,我不能呢?每一次都不是同一个单元格,如果我忽略和回忆表,可能是不同的单元格,也可能是相同的单元格。

我的configureCell函数工作正常,没有问题。(我知道这不是问题,因为我已经尝试过设置带有和不带函数的东西。)

我试过什么

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

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

我尝试将allowsSelectionisUserInteractionEnabled设置为tableView级别和tableViewCell级别。

2)我已经四次检查在单元格级别打开User Interaction Enabled,在表级别打开“单个选择”。

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

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

,这是我的代码:

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

代码语言:javascript
运行
复制
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()
    }

}
EN

Stack Overflow用户

发布于 2019-12-21 02:02:35

我不知道为什么会起作用,也不知道会有什么不同,但是我把tableView选择选项从“单一选择”切换到了“多重选择”,突然之间,它起了作用。它仍然可能是一个问题,我的电脑编译它,但这是我最后一次改变,它突然开始工作。

谢谢你的帮助!

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59348937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档