Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. import UIKit
  2.  
  3. let cellID = "cell"
  4.  
  5. class SettingViewController: UITableViewController {
  6. var selectedIndexPath : NSIndexPath?
  7.  
  8. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  9. return 1
  10. }
  11.  
  12. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  13. return 2
  14. }
  15.  
  16. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  17. let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
  18. cell.textLabel!.text = "Test Title"
  19. return cell
  20. }
  21.  
  22. override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  23. let previousIndexPath = selectedIndexPath
  24. if indexPath == selectedIndexPath {
  25. selectedIndexPath = nil
  26. }
  27. else {
  28. selectedIndexPath = indexPath
  29. }
  30.  
  31. var indexPaths : Array<NSIndexPath> = []
  32. if let previous = previousIndexPath {
  33. indexPaths += [previous]
  34. }
  35. if let current = selectedIndexPath {
  36. indexPaths += [current]
  37. }
  38. if indexPaths.count > 0 {
  39. tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic )
  40. }
  41. }
  42.  
  43. override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  44. (cell as! SettingViewCell).watchFrameChanges()
  45. }
  46.  
  47. override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  48. (cell as! SettingViewCell).ignoreFrameChanges()
  49. }
  50.  
  51. override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  52. if indexPath == selectedIndexPath {
  53. return SettingViewCell.expandedHeight
  54. }
  55. else {
  56. return SettingViewCell.defaultHeight
  57. }
  58. }
  59. }
  60.  
  61. import UIKit
  62.  
  63. class SettingViewCell : UITableViewCell {
  64.  
  65. @IBOutlet weak var titleLabel: UILabel!
  66. @IBOutlet weak var datePicker: UIDatePicker!
  67. class var expandedHeight: CGFloat { get {return 200 } }
  68. class var defaultHeight: CGFloat { get {return 44 } }
  69.  
  70. func checkHeight() {
  71. datePicker.hidden = (frame.size.height < SettingViewCell.expandedHeight)
  72. }
  73.  
  74. func watchFrameChanges() {
  75. addObserver(self, forKeyPath: "frame", options: .New, context: nil)
  76. checkHeight()
  77. }
  78.  
  79. func ignoreFrameChanges() {
  80.  
  81. removeObserver(self, forKeyPath: "frame")
  82. }
  83.  
  84. override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
  85. if keyPath == "frame" {
  86. checkHeight()
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement