Category Archives: Table View with Sections and Headings

Table View with Sections and Headings


import UIKit

class ViewController: UIViewController, UITableViewDataSource {
 let dailyTask = ["check all windows","check all doors","check temperature of freezer","check the mailbox at the end of the lane","Empty trash containers","if freezing, check water pines outside"]
 let weeklyTasks = ["check inside all unoccupied cabins","Run all faucets for 30 seconds","Arraunge for dumpster pickup"]
 let twoWeekTasks = ["run test on security alarm","check all motion detectors","Test smoke a alarm"]
 func numberOfSections(in tableView: UITableView) -> Int {
 return 3 // how many section have
 }
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 switch section {
 case 0:
 return dailyTask.count
 case 1:
 return weeklyTasks.count
 case 2:
 return twoWeekTasks.count
 default:
 return 0
 }
 }
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = UITableViewCell()
 var currentTask: String
 switch indexPath.section {
 case 0:
 currentTask = dailyTask[indexPath.row]
 case 1:
 currentTask = weeklyTasks[indexPath.row]
 case 2:
 currentTask = twoWeekTasks[indexPath.row]
 default:
 currentTask = ""
 }
 cell.textLabel!.text = currentTask
 return cell
 }
 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
 switch section {
 case 0:
 return "Daily Tasks"
 case 1:
 return "Weekly Tasks"
 case 2:
 return "Every Two Weeks"
 default:
 return "This Should Not Be Here" }
 }

 override func viewDidLoad() {
 super.viewDidLoad()
 // Do any additional setup after loading the view, typically from a nib.
 }

 override func didReceiveMemoryWarning() {
 super.didReceiveMemoryWarning()
 // Dispose of any resources that can be recreated.
 }

}