滚动屏幕时,上部标记消失。我在这里尝试了一些解决方案,但都没有用......
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = players[indexPath.row]
cell.textLabel?.textColor = UIColor.white
cell.backgroundColor = colorForIndex(index: indexPath.row)
cell.textLabel?.font = UIFont(name: "Chalkboard SE", size: 20)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath){
if (cell.accessoryType == .none) {
cell.accessoryType = .checkmark
let player = players[indexPath.row]
playersSelecteds.append(player)
} else {
cell.accessoryType = .none
let player = players[indexPath.row]
if let position = playersSelecteds.index(of: player) {
playersSelecteds.remove(at: position)
}
}
}
}
答案 0 :(得分:0)
你应该有一个检查标记单元格的模型。因为每次滚动时,都会根据tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
方法重新加载单元格。
因此,例如添加这样的模型:
var playersCheckmarked: [Bool] = []
然后在viewDidLoad中添加一些布尔值给这个数组,就像玩家数组有对象一样多。根据用户选择,override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
方法集playersCheckmarked[indexPath.row] = true
或playersCheckmarked[indexPath.row] = false
中的下一步。
当然,在override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
方法中添加以下行:
cell.accessoryType = playersCheckmarked[indexPath.row] == true .checkmark : .none