我有一个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显示错误的颜色。
答案 0 :(得分:1)
所以问题是我的CircleView类中缺少调用setNeedsDisplay()
。谢谢William GP。
答案 1 :(得分:1)
如果没有默认颜色,请使用Chain 1
Chain 1 Iteration 0
Chain 1 Iteration 1
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Calls: mi -> mi -> .local -> .mi -> write.table -> file
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file '/var/tmp/Rtmp0TqkWn/mi1502972500/pars_1.csv': No such file or directory
Execution halted
重置颜色以清除。此功能必须在prepareForReuse()
类
HistoryCell
答案 2 :(得分:0)
如果需要重置某个值,例如textColor
当单元格被重新出现以供重用时,您可以覆盖prepareForReuse
子类(HistoryCell)中的UITableViewCell
方法,如下所示:
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
}