swift - Swift Tableview 可重用单元格问题
问题描述
我对表格视图单元格中的可重用单元格有疑问,不幸的是我无法弄清楚如何强制单元格的状态。我很确定这是问题所在,因为当我重新加载 tableview 时,一切都正确显示。只有当我滚动时,我才开始看到问题,如果我再次重新加载显示会自行纠正。
这是正确的显示:
滚动后显示不正确:
我的 cellForRowAt 代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "historyGoalCell", for: indexPath)
let name = items[indexPath.section][indexPath.row].name
let date = dateManager.dateAsString(for: items[indexPath.section][indexPath.row].date!)
if tempDate != date {
// show header
cell.textLabel?.text = date
tempDate = date
} else {
// don't show header
cell.textLabel?.text = ""
}
cell.detailTextLabel?.text = "\(date),\(name ?? "")"
return cell
}
感谢您的帮助,我已经坚持了几天,对 TableViews 非常陌生-谢谢
解决方案
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
可以按任何顺序调用。它不是从 1 到 N 的一致调用,因此逻辑tempDate
工作不按计划进行。最好做一些前期工作,并在放置标题的位置创建一个带有索引的数组。例如
struct Pair : Hashable {
var i : Int
var j : Int
}
//Somewhere one time before the first reloadData
var hasIndex : Set<Pair> = []
var tempDate: Date = Date.distantPast
for i in 0..<sections {
for j in 0..<rows[i] {
let name = items[i][j].name
let date = dateManager.dateAsString(for: items[i][j].date!)
if tempDate != date {
hasIndex.insert(Pair(i: i, j: j))
// OR items[i][j].showHeader = true
tempDate = date
} else {
// OR items[i][j].showHeader = false
}
}
}
...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "historyGoalCell", for: indexPath)
let name = items[indexPath.section][indexPath.row].name
let date = dateManager.dateAsString(for: items[indexPath.section][indexPath.row].date!)
if hasIndex.contains(Pair(i: indexPath.section, j: indexPath.row)) {
// OR if items[indexPath.section][indexPath.row].showHeader {
cell.textLabel?.text = date
tempDate = date
} else {
cell.textLabel?.text = ""
}
cell.detailTextLabel?.text = "\(date),\(name ?? "")"
return cell
}