【发布时间】: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有时不正确? (这就是图片显示的内容,我只是问这种行为是否一致) -
我没有注意到,但确实如此!这种行为是一致的。
-
顺便问一下,
circleView和ringColor是什么?是UIView还是borderColor?或者是其他东西?它的颜色是如何设置的?
标签: ios swift uitableview