Swift Tableview 可重用单元格问题

Swift Tableview reusable cell issue

我认为 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 的逻辑未按计划工作。最好做一些 pre-work 并在放置 headers 的位置创建一个带有索引的数组。例如

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
}