Skip to content

Instantly share code, notes, and snippets.

@DylanVann
Created June 4, 2015 16:38
Show Gist options
  • Save DylanVann/e1674e3f1d959afd0fa2 to your computer and use it in GitHub Desktop.
Save DylanVann/e1674e3f1d959afd0fa2 to your computer and use it in GitHub Desktop.
A UITableViewController that displays a DatePickerCell and a default UITableViewCell.
import UIKit
import DatePickerCell
class ViewController: UITableViewController {
var cells:NSArray = []
override func viewDidLoad() {
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44
// The DatePickerCell.
let datePickerCell = DatePickerCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
// -------------------------------------------------------------- Here it is.
let otherStaticCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
// Cells is a 2D array containing sections and rows.
// -------------------------------------------------------------- I'm putting cells in this array.
cells = [[datePickerCell, otherStaticCell]]
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// Get the correct height if the cell is a DatePickerCell.
var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
if (cell.isKindOfClass(DatePickerCell)) {
return (cell as! DatePickerCell).datePickerHeight()
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Deselect automatically if the cell is a DatePickerCell.
var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)
if (cell.isKindOfClass(DatePickerCell)) {
var datePickerTableViewCell = cell as! DatePickerCell
datePickerTableViewCell.selectedInTableView(tableView)
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return cells.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cells[section].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// -------------------------------------------------------------- Returning the cell from the cells array here.
return cells[indexPath.section][indexPath.row] as! UITableViewCell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment