RxDataSources not updating section header title

3k views Asked by At

I'm using RxDataSources to load and display a UITableview. I am trying to update the section header with the amount of items that it holds, however tough the cell and items update correctly, the title remains stale.

This is my code for the DataSource object:

tableViewDataSource = RxTableViewSectionedAnimatedDataSource<TableViewParticipationSection>(
           configureCell: { (_, tableView, _, item) in
               return TableViewCellType.transformData(item).cell(inTableView: tableView)
       }, titleForHeaderInSection: { dataSource, index in
           let sectionModel = dataSource.sectionModels[index]
           return "\(sectionModel.items.count)"
           })

The identity of the section header is just {return 0} since I only have a single section.

Furthermore I have confirmed that if I use this code:

DispatchQueue.main.asyncAfter(deadline: .now()+3, execute: {
               self?.contentView.tableView.reloadData()
           })

It will actually update the section title, so it seems to be some problem with staleness but I can't seem to track it down.

Does anyone have experience with dynamic titles using RxDataSources

Edit: After further experiments, the title will update, if I scroll around in the tableview, the title changes at some point.

2

There are 2 answers

0
Yoav Schwartz On

Turns out that the title or any data on the section model is not included in the diff, so no matter what you do, it won't make a difference. The RxDataSource doesn't support non static headers. The solution is to make a custom view and do the binding myself.

0
Jan On

In my case I set new empty UIView for section in

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if section == 0 {
        let customHeaderViewMulti = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderView") as! CustomHeaderView
            return customHeaderView
        }

    // This is WRONG:
    return UIView()
}

You must return nil for other cases.