Advertisement
Larme

Untitled

May 13th, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.37 KB | None | 0 0
  1. struct GlossaryModel {
  2.     let key: String
  3.     let offences: [Offence]
  4. }
  5.  
  6. class GlossaryVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
  7.  
  8.     private var datasource = [GlossaryModel]()
  9.     private var filteredDataSource = [GlossaryModel]()
  10.     override func viewDidAppear(_ animated: Bool) {
  11.  
  12.         DataProvider.getData(with: .GLOSSARY_DATA_URL) { [weak self] (letters) in
  13.             guard let self = self else { return }
  14.             let allOffences = letters.flatMap { $0.offences }
  15.            
  16.             //Construct your oldDict, code ~ in groupGlossarys
  17.             let glossarysDictionary: [String: [Offence]] = [:]
  18.            
  19.             //Then:
  20.             self.datasource = glossarysDictionary.compactMap{
  21.                 let sortedOffences = $0.value.sorted(by: { $0.valueForSorting < $1.valueForSorting })
  22.                 GlossaryModel(key: $0.key,
  23.                               offences: sortedOffences)
  24.             }
  25.             self.filteredDataSource = self.datasource
  26.         }
  27.  
  28.     }
  29.  
  30.     func groupGlossarys() {
  31.          ...
  32.         checkAndShowTableView()
  33.     }
  34.  
  35.     func checkAndShowTableView() {
  36.        
  37.         if filteredDataSource.isEmpty == false { //prefers using isEmpty instead of count
  38.             tableView.isHidden = false
  39.             noResultsView.isHidden = true
  40.             tableView.reloadData()
  41.         } else {
  42.             tableView.isHidden = true
  43.             noResultsView.isHidden = false
  44.         }
  45.     }
  46.  
  47.     func numberOfSections(in tableView: UITableView) -> Int {
  48.         return filteredDataSource.count
  49.     }
  50.  
  51.     func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  52.         return filteredDataSource[section].key
  53.     }
  54.  
  55.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  56.         return filteredDataSource[section].offences.count
  57.     }
  58.  
  59.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  60.  
  61.         guard let glossaryCell = tableView.dequeueReusableCell(withIdentifier: glossaryCellId, for: indexPath) as? GlossaryCell else {
  62.             fatalError("Unable to dequeue contact cell")
  63.         }
  64.  
  65.         let offence = filteredDataSource[indexPath.section].offences[indexPath.row]
  66.         glossaryCell.configure(glossary: offence)
  67.         return glossaryCell
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement