我有一个 UITableViewController 显示带有一些标签和自定义 UIView 的自定义单元格。在 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 方法中,当单元格被重用时,颜色似乎没有被重置(它们看起来完全随机)。我怎样才能解决这个问题?

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let historyEntry = allHistoryEntries[indexPath.section].histories![indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! HistoryCell

    cell.dateLabel.text = "\(getDayFrom(date: (historyEntry.beginDate)!))"

    let highestBac = getHighestBac(history: historyEntry)
    cell.highestBacLabel.text = "Høyeste promille " + String(describing: Double(highestBac).roundTo(places: 2))

    cell.costLabel.text = String(describing: getNorwegianDayFrom(date: (historyEntry.beginDate!))) + " brukte du " + String(describing: calculateTotalCostBy(history: historyEntry)) + ",-"

    let goal = Double(AppDelegate.getUserData()?.goalPromille ?? 0.0)
    let red = UIColor(red: 193/255.0, green: 26/255.0, blue: 26/255.0, alpha: 1.0)
    let green = UIColor(red:26/255.0, green: 193/255.0, blue: 73/255.0, alpha: 1.0)

    let color = highestBac > goal ? red : green

    cell.highestBacLabel.textColor = color
    cell.circleView.ringColor = color

    return cell
}

这是显示颜色的图像。预期的行为是应该使用红色或绿色,而不是组合。

更新:
只有 ringColor 显示错误的颜色。

ios - 自定义 UITableViewCell 显示不正确的值-LMLPHP

最佳答案

如果没有默认颜色,请使用 prepareForReuse() 重置颜色以清除。此函数必须在您的 HistoryCell 类中

override func prepareForReuse() {
    super.prepareForReuse()
    //Reset label to clear color
    self.highestBacLabel.textColor = UIColor.clear
}

关于ios - 自定义 UITableViewCell 显示不正确的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45765688/

10-14 22:40
查看更多