David
David

Reputation: 3418

UIDatePicker in UITableView, ensure Visibility

I have a static UITableView with several sections. In one table row I have a UIDatePicker. On touch the table cell expands and I can select the date. Fine so far. But if the table row is on the bottom of the page I need to manually scroll up to select a date. How can I ensure the datepicker to be in view like the calendar app does? Can you please point me into the right direction?

Upvotes: 1

Views: 135

Answers (2)

Kaptain
Kaptain

Reputation: 1348

You can use this function: func scrollToRowAtIndexPath(indexPath: NSIndexPath, atScrollPosition scrollPosition: UITableViewScrollPosition, animated: Bool)

Use it in

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            // ...
            var indexPathToJump = NSIndexPath(forRow: 0, inSection: 5)
            tableView .scrollToRowAtIndexPath( indexPathToJump, atScrollPosition: .None, animated: true)
        }
    }

Upvotes: 2

Undo
Undo

Reputation: 25697

Use scrollToRowAtIndexPath:atScrollPosition:animated::

self.tableView.scrollToRowAtIndexPath(myIndexPath, scrollPosition: .None, animated: true)

I haven't tested this syntax, please let me know if it needs improved, but I know that the method is right. You want to use UITableViewScrollPosition.None so that it move the table view just enough that the row in question is in view:

UITableViewScrollPositionNone

The table view scrolls the row of interest to be fully visible with a minimum of movement. If the row is already fully visible, no scrolling occurs. For example, if the row is above the visible area, the behavior is identical to that specified by UITableViewScrollPositionTop. This is the default.

Available in iOS 2.0 and later.

Upvotes: 1

Related Questions