Advertisement
irobust

iOS Table View with Section

Aug 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.51 KB | None | 0 0
  1. // 1.
  2. override func numberOfSections(in tableView: UITableView) -> Int {
  3.         return 3
  4. }
  5.  
  6. // 2.
  7. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  8.         switch section {
  9.         case 0:
  10.             return self.dailyTasks.count
  11.         case 1:
  12.             return self.weeklyTasks.count
  13.         case 2:
  14.             return self.monthlyTasks.count
  15.         default:
  16.             return 0
  17.         }
  18. }
  19.  
  20. // 3.
  21. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  22.        
  23.         // let cell = UITableViewCell()
  24.         let cell = tableView.dequeueReusableCell(withIdentifier: "normalcell")!
  25.        
  26.         switch indexPath.section {
  27.         case 0:
  28.             cell.textLabel?.text = self.dailyTasks[indexPath.row]
  29.         case 1:
  30.             cell.textLabel?.text = self.weeklyTasks[indexPath.row]
  31.         case 2:
  32.             cell.textLabel?.text = self.monthlyTasks[indexPath.row]
  33.         default:
  34.             cell.textLabel?.text = "No data"
  35.         }
  36.         return cell
  37.     }
  38.  
  39. // 4.
  40. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  41.         switch section {
  42.         case 0:
  43.             return "Daily Tasks"
  44.         case 1:
  45.             return "Weekly Tasks"
  46.         case 2:
  47.             return "Monthly Tasks"
  48.         default:
  49.             return nil
  50.         }
  51.     }
  52.  
  53. // 5.
  54. Storyboard -> (Table View) change style from plain to grouped
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement