I have a settings page with a static table where users can select the times they are available to work for each weekday (one section per weekday).
To not have to write the same code for each weekday, I created an array of dictionaries called datepickers
that allows me to apply the same logic to override func tableView
for multiple days. The array is set up in way so that it can access the date pickers after the class has been initialised.
Coming from a Ruby background where we use dynamic variables and don't explicitly declare types of variables, I'm unsure how to type this var datePickers : [???]
. What's the correct way of typing this variable and more generally speaking, is my approach correct to refactor this with an array of dictionaries correct?
import UIKit
class AvailabilityController: UITableViewController {
@IBOutlet weak var mondaysFromDatePicker: UIDatePicker!
@IBOutlet weak var mondaysToDatePicker: UIDatePicker!
@IBOutlet weak var tuesdaysFromDatePicker: UIDatePicker!
@IBOutlet weak var tuesdaysToDatePicker: UIDatePicker!
var datePickers : [???] = {
get {
return [
["section": 0, "row": 2, "datePicker": self.mondaysFromDatePicker],
["section": 0, "row": 4, "datePicker": self.mondaysToDatePicker],
["section": 1, "row": 2, "datePicker": self.tuesdaysFromDatePicker],
["section": 1, "row": 4, "datePicker": self.tuesdaysToDatePicker]
]
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
for datePicker in datePickers {
if indexPath.section == datePicker["section"]! && indexPath.row == datePicker["row"]! {
let height:CGFloat = datePicker["datePicker"]!.hidden ? 0.0 : 216.0
return height
}
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
}