Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct GlossaryModel {
- let key: String
- let offences: [Offence]
- }
- class GlossaryVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
- private var datasource = [GlossaryModel]()
- private var filteredDataSource = [GlossaryModel]()
- override func viewDidAppear(_ animated: Bool) {
- DataProvider.getData(with: .GLOSSARY_DATA_URL) { [weak self] (letters) in
- guard let self = self else { return }
- let allOffences = letters.flatMap { $0.offences }
- //Construct your oldDict, code ~ in groupGlossarys
- let glossarysDictionary: [String: [Offence]] = [:]
- //Then:
- self.datasource = glossarysDictionary.compactMap{
- let sortedOffences = $0.value.sorted(by: { $0.valueForSorting < $1.valueForSorting })
- GlossaryModel(key: $0.key,
- offences: sortedOffences)
- }
- self.filteredDataSource = self.datasource
- }
- }
- func groupGlossarys() {
- ...
- checkAndShowTableView()
- }
- func checkAndShowTableView() {
- if filteredDataSource.isEmpty == false { //prefers using isEmpty instead of count
- tableView.isHidden = false
- noResultsView.isHidden = true
- tableView.reloadData()
- } else {
- tableView.isHidden = true
- noResultsView.isHidden = false
- }
- }
- func numberOfSections(in tableView: UITableView) -> Int {
- return filteredDataSource.count
- }
- func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- return filteredDataSource[section].key
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return filteredDataSource[section].offences.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- guard let glossaryCell = tableView.dequeueReusableCell(withIdentifier: glossaryCellId, for: indexPath) as? GlossaryCell else {
- fatalError("Unable to dequeue contact cell")
- }
- let offence = filteredDataSource[indexPath.section].offences[indexPath.row]
- glossaryCell.configure(glossary: offence)
- return glossaryCell
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement