Post

Replies

Boosts

Views

Activity

Reply to Section titles with UITableViewDiffableDataSource
The secret is on Data Model, I think that you must have the title for the section on your Model. Imagine the following solution: You have this Section Model: struct Section: Hashable { let name: String let listOfItems: [String] let uuid = UUID() } So on your view controller, you will have the dataSource like that: var dataSource: UITableViewDiffableDataSource<Section, String> Then, on implementation of your updateDataSource you will use like that: private func updateDataSource(sections: [Section]) {     var snapshot = NSDiffableDataSourceSnapshot<Section, String>()           snapshot.appendSections(sections)           for section in sections {       snapshot.appendItems(section.listOfItems, toSection: section)     }           dataSource.apply(snapshot)  } So, to set the section title I have used the implementation of tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? creating a label and returning it, to set the text of the label I have used the dataSource function sectionIdentifier(for index: Int) -> SectionIdentifierType?, unfortunatelly this function is available only on iOS, tvOS 15.0 or later. override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {     let label = UILabel(frame: CGRect(x: 0, y: 0, width: 60, height: 20))     label.text = dataSource.sectionIdentifier(for: section)?.name     return label } The function sectionIdentifier(for index: Int) -> SectionIdentifierType? returns the type of the Section Identifier defined on dataSource, in this example the type of the section identifier is Section, so, just use the name property to set the name of your section. I hope that this explanation could help you. 🤘🏼🤘🏼😉😉
Dec ’21