I have a UITableView
with custom section headers implemented as UITableViewCell
s. The relevant method from my UITableViewDelegate
looks like this:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
log.debug("Creating header for section \(section)")
let headerCell = tableView.dequeueReusableCell(withIdentifier: "SectionHeaderCell") as! JournalEntrySectionHeaderTableViewCell
let keys = Array(self.journalEntries.keys.sorted())
let date = keys[section]
let dateFormatter = DateFormatter()
let template = "MMMMd, yyyy"
dateFormatter.setLocalizedDateFormatFromTemplate(template)
let dateStr = dateFormatter.string(from: date)
headerCell.titleLabel?.text = dateStr
let theme = ThemeManager.shared.currentTheme()
headerCell.backgroundColor = theme.entryListHeaderBackgroundColor
headerCell.titleLabel?.textColor = theme.entryListHeaderTextColor
return headerCell
}
Everything renders properly, and looks like this:
However, when I go to remove a UITableViewCell
by calling self.entryTableView?.deleteRows(at: [indexPath], with: .automatic)
, after the animation is performed the section header is not rendered properly. This is the resulting rendering:
You'll notice in this screenshot that I deleted a row from the March 9, 2019 section. After doing so, the section header for the March 26, 2019 section is not rendered at all. If I scroll the table down and then scroll back up, the section headers are rendered properly, as expected.
I've never seen anything like this before, and it does not happen if I remove my custom viewForHeaderInSection
implementation and simply make use of titleForHeaderInSection
. However, that's not an option, because I need to change the background color of the section header to match our UI theme. Has anyone encountered this before? Any idea what the issue could be?