Friday, June 7, 2013

UITableView UITableViewScrollPositionBottom example in Objective C (iOS).


UITableView UITableViewScrollPositionBottom

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 UITableViewScrollPositionBottom]
You set the scroll position through a parameter of the selectRowAtIndexPath:animated:scrollPosition:, scrollToNearestSelectedRowAtScrollPosition:animated:, cellForRowAtIndexPath:, and indexPathForSelectedRow methods.
UITableView UITableViewScrollPositionBottom example.
// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (dataHasChanged) {
        self.dataHasChanged = NO;
        [[self tableView] reloadData];
    } else {
        self.scrollToLast = NO; // No reload -> no need to scroll!
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (scrollToLast) {
        NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
        [[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}

Example of [UITableView UITableViewScrollPositionBottom].
[theTableView scrollToRowAtIndexPath:
                [NSIndexPath indexPathForRow:savedScrollPosition inSection:0]
                atScrollPosition:UITableViewScrollPositionBottom
                animated:NO];

UITableView UITableViewScrollPositionBottom example.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// on the initial cell load scroll to the last row (ie the latest Note)
if (initialLoad==TRUE) {
    initialLoad=FALSE;
    NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([self.tableView numberOfRowsInSection:0] - 1) inSection:0];
    [[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
        CGPoint offset = CGPointMake(0, (1000000.0));
        [self.tableView setContentOffset:offset animated:NO];
    }
}

End of UITableView UITableViewScrollPositionBottom example article.