Friday, June 7, 2013

UITableView UITableViewScrollPositionTop example in Objective C (iOS).


UITableView UITableViewScrollPositionTop

Table View Scroll Position
The position in the table view (top, middle, bottom) to which a given row is scrolled.

typedef enum {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
} UITableViewScrollPosition;

Constants
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.
UITableViewScrollPositionTop
The table view scrolls the row of interest to the top of the visible table view.
UITableViewScrollPositionMiddle
The table view scrolls the row of interest to the middle of the visible table view.
UITableViewScrollPositionBottom
The table view scrolls the row of interest to the bottom of the visible table view.

Discussion of [UITableView UITableViewScrollPositionTop]
You set the scroll position through a parameter of the selectRowAtIndexPath:animated:scrollPosition:, scrollToNearestSelectedRowAtScrollPosition:animated:, cellForRowAtIndexPath:, and indexPathForSelectedRow methods.
UITableView UITableViewScrollPositionTop example.
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[yourTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
atScrollPosition could take any of these values:


Example of [UITableView UITableViewScrollPositionTop].
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:position inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

UITableView UITableViewScrollPositionTop example.
[tableView reloadData];
int lastRowNumber = [tableView numberOfRowsInSection:0] - 1;
NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
[tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];

End of UITableView UITableViewScrollPositionTop example article.