我有一个tableview,列出了与游戏相关的人。当玩家被淘汰时,我会运行以下代码:
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次给了我一个致命错误:索引超出范围并指向此特定行在代码中:
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
}
我不确定如何解决这个问题。我做“打印”所有数组的新值以防万一,我确认它们都是正确的(现在它们是2,它们是2)。然后,当它运行表的更新时,它会崩溃,认为indexPath.row中有第3个元素? (我认为这是发生的事情)。 IndexPath.row是不是更新了什么?有没有办法更正确地更新它?
答案 0 :(得分:1)
如果在主线程上调用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()
}