Friday, June 7, 2013

UITableView UITableViewScrollPositionNone example in Objective C (iOS).


UITableView UITableViewScrollPositionNone

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 UITableViewScrollPositionNone]
You set the scroll position through a parameter of the selectRowAtIndexPath:animated:scrollPosition:, scrollToNearestSelectedRowAtScrollPosition:animated:, cellForRowAtIndexPath:, and indexPathForSelectedRow methods.
UITableView UITableViewScrollPositionNone example.
If you have no sections, then you can try the indexPathWithIndex: class constructor:

[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathWithIndex:1]
                      atScrollPosition:UITableViewScrollPositionNone
                              animated:NO];

Example of [UITableView UITableViewScrollPositionNone].
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardFrame.size.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
[self.tableView scrollToRowAtIndexPath:self.currentEditingCellIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];

UITableView UITableViewScrollPositionNone example.
 + (NSInteger)loadMore : (NSInteger)numberOfItemsToDisplay arrayTemp:(NSMutableArray*)aryItems tblView:(UITableView*)tblList
    {
        int count =0;
        NSUInteger i, totalNumberOfItems = [aryItems count];
        NSUInteger newNumberOfItemsToDisplay = MIN(totalNumberOfItems, numberOfItemsToDisplay + kNumberOfItemsToAdd);
        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
        for (i=numberOfItemsToDisplay; i<newNumberOfItemsToDisplay; i++)
        {
            count++;
            [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }
        numberOfItemsToDisplay = newNumberOfItemsToDisplay;
        [tblList beginUpdates];
        [tblList insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
        if (numberOfItemsToDisplay == totalNumberOfItems) {
            [tblList deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationLeft];
        }
        NSIndexPath *scrollPointIndexPath;
        if (newNumberOfItemsToDisplay < totalNumberOfItems)
        {
            scrollPointIndexPath = [NSIndexPath indexPathForRow:numberOfItemsToDisplay-kNumberOfItemsToAdd inSection:0];
        }
        else
        {
            scrollPointIndexPath = [NSIndexPath indexPathForRow:i-count inSection:0];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100000000), dispatch_get_main_queue(), ^(void){
            [tblList scrollToRowAtIndexPath:scrollPointIndexPath atScrollPosition:UITableViewScrollPositionNone  animated:YES];
        });
        return numberOfItemsToDisplay;
    }

End of UITableView UITableViewScrollPositionNone example article.