0

I have a UITableView with custom section headers implemented as UITableViewCells. 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:

Properly rendered UITableView

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:

Incorrectly rendered UITableView

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?

Shadowman
  • 11,150
  • 19
  • 100
  • 198
  • Can you please add code for function where you call `self.entryTableView?.deleteRows(at: [indexPath], with: .automatic)`? – Bhaumik Mar 01 '19 at 20:50

1 Answers1

0

I may think there could be 2 potential issue.

  1. Could you verify if you remove dataSource element before call

    self.entryTableView?.deleteRows(at: [indexPath], with: .automatic)

  2. let keys = Array(self.journalEntries.keys.sorted())

This keys changes after you remove the element? Which's could lead to unexpected behavior.

batphonghan
  • 487
  • 3
  • 10