Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- let cellID = "cell"
- class SettingViewController: UITableViewController {
- var selectedIndexPath : NSIndexPath?
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return 2
- }
- override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)
- cell.textLabel!.text = "Test Title"
- return cell
- }
- override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
- let previousIndexPath = selectedIndexPath
- if indexPath == selectedIndexPath {
- selectedIndexPath = nil
- }
- else {
- selectedIndexPath = indexPath
- }
- var indexPaths : Array<NSIndexPath> = []
- if let previous = previousIndexPath {
- indexPaths += [previous]
- }
- if let current = selectedIndexPath {
- indexPaths += [current]
- }
- if indexPaths.count > 0 {
- tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Automatic )
- }
- }
- override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
- (cell as! SettingViewCell).watchFrameChanges()
- }
- override func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
- (cell as! SettingViewCell).ignoreFrameChanges()
- }
- override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
- if indexPath == selectedIndexPath {
- return SettingViewCell.expandedHeight
- }
- else {
- return SettingViewCell.defaultHeight
- }
- }
- }
- import UIKit
- class SettingViewCell : UITableViewCell {
- @IBOutlet weak var titleLabel: UILabel!
- @IBOutlet weak var datePicker: UIDatePicker!
- class var expandedHeight: CGFloat { get {return 200 } }
- class var defaultHeight: CGFloat { get {return 44 } }
- func checkHeight() {
- datePicker.hidden = (frame.size.height < SettingViewCell.expandedHeight)
- }
- func watchFrameChanges() {
- addObserver(self, forKeyPath: "frame", options: .New, context: nil)
- checkHeight()
- }
- func ignoreFrameChanges() {
- removeObserver(self, forKeyPath: "frame")
- }
- override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
- if keyPath == "frame" {
- checkHeight()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement