簡體   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