Friday, June 7, 2013

UITableView UITableViewScrollPositionMiddle example in Objective C (iOS).


UITableView UITableViewScrollPositionMiddle

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 UITableViewScrollPositionMiddle]
You set the scroll position through a parameter of the selectRowAtIndexPath:animated:scrollPosition:, scrollToNearestSelectedRowAtScrollPosition:animated:, cellForRowAtIndexPath:, and indexPathForSelectedRow methods.
UITableView UITableViewScrollPositionMiddle example.
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //Center previously ticked cell to center of the screen
    [self.tableView scrollToRowAtIndexPath:yourSelectedIndexMethod  atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

Example of [UITableView UITableViewScrollPositionMiddle].
You are right to identify the scrollToRowAtIndexPath method. So all you need to do is create a fresh IndexPath with the row you wish to move to, e.g., row index = 10:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:10 inSection:indexPath.section]
             atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

UITableView UITableViewScrollPositionMiddle example.
tabelview.contentInset =  UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
                 atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

End of UITableView UITableViewScrollPositionMiddle example article.