gpt4 book ai didi

ios - UITableView:IndexPath.row 超出范围( fatal error )

转载 作者:行者123 更新时间:2023-11-28 15:01:58 25 4
gpt4 key购买 nike

我有一个表格 View ,其中列出了与游戏相关的人员。当玩家被淘汰时,我运行以下代码:

func doRemovePlayer(playerDed: GKPlayer) {

print("\n ** Removing \(playerDed.alias!) from the game! ** \n")
gameLog += "Removing \(playerDed.alias!) from the game!"
let elIndex = (gKPlayerArray.index(of: playerDed))!
scoreArray.remove(at: elIndex)
randomNoArray.remove(at: elIndex)
timeResultsArray.remove(at: elIndex)
answerResultArray.remove(at: elIndex)
playersReadyForNextStage.remove(at: elIndex)
gKPlayerArray.remove(at: elIndex)
noOfPlayers -= 1
print ("New Array counts are: \(scoreArray.count), \(randomNoArray.count), \(timeResultsArray.count), \(answerResultArray.count), \(playersReadyForNextStage.count), \(gKPlayerArray.count), Players: \(noOfPlayers)")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: {
self.scoreBoardTable.reloadData()
})
checkIfWinnerOrLoser()
}

如您所见,我做了一个调度队列只是为了避免任何问题(我认为)。无论如何,当我在 2 个模拟器和我的实际 iPhone 上运行此代码时,模拟器对此代码没有问题,但是 iPhone 3/4 次给我一个 fatal error :索引超出范围并指向此特定行在代码中:

extension GameScreenVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return gKPlayerArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let scoreCell = tableView.dequeueReusableCell(withIdentifier: "playerCell") as! ScoreBoardTableCell
**scoreCell.nameLabel.text = gKPlayerArray[indexPath.row].alias!** //FATAL ERROR: INDEX OUT OF RANGE
scoreCell.scoreLabel.text = String(scoreArray[indexPath.row])
if indexPath.row == 0 {
scoreCell.layer.borderWidth = 3
scoreCell.layer.borderColor = UIColor.blue.cgColor
} else {
scoreCell.layer.borderWidth = 0
}
scoreCell.statusLabel.layer.cornerRadius = 7
if playersReadyForNextStage[indexPath.row] {
scoreCell.statusLabel.layer.backgroundColor = UIColor.yellow.cgColor
} else {
scoreCell.statusLabel.layer.backgroundColor = UIColor.green.cgColor
}
return scoreCell
}

我不确定如何解决这个问题。为了以防万一,我“打印”了所有数组的新值,并且我确认它们都是正确的(它们是 3 现在是 2)。然后,当它为表运行更新时,它以为 indexPath.row 中有第三个元素而崩溃了? (我认为这就是正在发生的事情)。IndexPath.row 是不是没有更新之类的?有没有办法更正确地更新它?

最佳答案

如果在主线程上调用了 doRemovePlayer,您就没有理由稍后调度重新加载。试试这个:

func doRemovePlayer(playerDed: GKPlayer) {

print("\n ** Removing \(playerDed.alias!) from the game! ** \n")
gameLog += "Removing \(playerDed.alias!) from the game!"
let elIndex = (gKPlayerArray.index(of: playerDed))!
scoreArray.remove(at: elIndex)
randomNoArray.remove(at: elIndex)
timeResultsArray.remove(at: elIndex)
answerResultArray.remove(at: elIndex)
playersReadyForNextStage.remove(at: elIndex)
gKPlayerArray.remove(at: elIndex)
noOfPlayers -= 1
print ("New Array counts are: \(scoreArray.count), \(randomNoArray.count), \(timeResultsArray.count), \(answerResultArray.count), \(playersReadyForNextStage.count), \(gKPlayerArray.count), Players: \(noOfPlayers)")
// it should be enough to delete the corresponding row
scoreBoardTable.deleteRows(at: [IndexPath(row: elIndex, section: 0)], with: .automatic)
checkIfWinnerOrLoser()
}

关于ios - UITableView:IndexPath.row 超出范围( fatal error ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com