【问题标题】:Custom UITableViewCell showing incorrect values自定义 UITableViewCell 显示不正确的值
【发布时间】:2017-08-18 22:16:35
【问题描述】:

我有一个 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 显示错误的颜色。

【问题讨论】:

  • “当细胞被重复使用时”是什么意思?你是如何重复使用它们的?
  • 我可能是错的,但据我了解,当单元格不再出现在屏幕上时,它们会被重复使用,例如,如果屏幕上有更多单元格可以容纳。所以我怀疑在滚动表格视图时重复使用单元格,保持最初设置的颜色。
  • 是不是textColor 变化正确,但circleView.ringColor 有时不正确? (这就是图片显示的内容,我只是问这种行为是否一致)
  • 我没有注意到,但确实如此!这种行为是一致的。
  • 顺便问一下,circleViewringColor 是什么?是UIView 还是borderColor?或者是其他东西?它的颜色是如何设置的?

标签: ios swift uitableview


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    所以问题原来是在我的 CircleView 类中缺少调用 setNeedsDisplay()。谢谢威廉全科医生。

    【讨论】:

      【解决方案3】:

      如果您需要重置某些值,例如当单元格被出列以供重用时重置 textColor,您可以覆盖 UITableViewCell 子类 (HistoryCell) 中的 prepareForReuse 方法,如下所示:

      override func prepareForReuse() {
          super.prepareForReuse() 
          //Reset label color back to default green
          let green = UIColor(red:26/255.0, green: 193/255.0, blue: 73/255.0, alpha: 1.0)
          self.highestBacLabel.textColor = green
      }
      

      【讨论】:

      • 我更新了我的问题,使其更加具体。没有默认颜色,因为颜色由最高Bac > 目标确定。滚动时颜色只是随机显示。
      • @SimenFonnes 你能显示你的getHighestBacAppDelegate.getUserData() 函数的代码吗?我猜你那里有一些问题。乍一看,为什么不直接引用 getUserData().goalPromille,也许是在 viewDidLoad,这样您就不必继续返回 AppDelegate 并为每个单元格调用该函数?
      猜你喜欢
      • 2020-12-13
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多