我有一個UITableViewController顯示自定義單元格與幾個標籤和自定義UIView。在tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)方法中,當單元格被重用時(它們看起來完全是隨機的),顏色似乎沒有被重置。我怎樣才能解決這個問題?顯示不正確的值的自定義UITableViewCell
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
}
這是顯示顏色的圖像。預期的行爲是應該使用紅色或綠色,而不是組合。
UPDATE: 只有ringColor顯示錯誤的顏色。
您是什麼意思的「當細胞被重複使用」?你如何重複使用它們? –
我可能是錯的,但根據我的理解,細胞在不再出現在屏幕上時會被重用,例如,如果有更多細胞可以放入屏幕中。所以我懷疑這個單元格是在tableview滾動時重用的,它保留了最初設置的顏色。 – mrfonnes
「textColor」是否正確更改,但「circleView.ringColor」有時不正確? (這是圖片顯示的內容,我只是問這個行爲是否一致) –