Home > Mobile >  Is there a way to match Number of Sections match Dictionary Keys count in UITableViewDiffableDataSou
Is there a way to match Number of Sections match Dictionary Keys count in UITableViewDiffableDataSou

Time:05-02

I am trying to Group names by first Character and display it in ViewController using the new UITableViewDiffableDataSource.(e.g. Contacts Application)

**A** (header title) 
Apple 
Amazon
**B** (header title)
Broadcom
Bezoz
**C** (header title)
Calderon 

Below is the code I have using the old DataSource approach.

class ViewController: UITableViewDelegate, UITableViewDataSource {
    var tableView = UITableView()
    
    // Data Set for TableView
    var dict = [Character: [Contacts]]() // e.g. ["A": ["Apple", "Amazon"], "B": ["Broadcom, Bezoz"]...]
    var sortedDictKeys = [Character]() // e.g. ["A", "B", "C"]
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dict[sortedDictKeys[section]].count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return sortedDictKeys.count // returns 3 
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return String(viewModel.sortedBreedsKeys[section])
    }

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        let header = view as! UITableViewHeaderFooterView
        header.textLabel?.text = sortedDictKeys[section]
    }
}

Using old UITableViewDataSource, I get the view, but how do I set it using the Diffable approach. Is there a way to set the section and rows?

class ViewController: UITableViewDelegate {
    enum Section: CaseIterable {
        case main
    }

    var tableView = UITableView()

    // Data Set for TableView
    var dict = [Character: [Contacts]]() // e.g. ["A": ["Apple", "Amazon"], "B": ["Broadcom, Bezoz"]...]
    var sortedDictKeys = [Character]() // e.g. ["A", "B", "C"]

    var dataSource: UITableViewDiffableDataSource<Section, Contacts>!
    var snapshot = NSDiffableDataSourceSnapshot<Section, Contacts>()

    func viewDidLoad() {
 
        dataSource = UITableViewDiffableDataSource.init(tableView: self.tableView, cellProvider: { tableView, indexPath, contact in
            return UITableViewCell()
        })
    }

    // MARK - TODO - How do I define the snapshot and append sections to reuse section in the snapshot?
}

Appreciate the help! 

CodePudding user response:

Delete the Section enum. The sections are the sorted keys.

Declare the dataSource with this types

var dataSource: UITableViewDiffableDataSource<Character, Contacts>!

and create the snapshot

var snapshot = NSDiffableDataSourceSnapshot<Character, Contacts>()
snapshot.appendSections(sortedDictKeys)
sortedDictKeys.forEach { letter in
    snapshot.appendItems(dict[letter]!, toSection: letter)
}
  • Related