Friday, June 7, 2013

UITableView scrollToNearestSelectedRowAtScrollPosition example in Objective C (iOS).


UITableView scrollToNearestSelectedRowAtScrollPosition

Scrolls the table view so that the selected row nearest to a specified position in the table view is at that position.

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated

Parameters of [UITableView scrollToNearestSelectedRowAtScrollPosition]
scrollPosition
A constant that identifies a relative position in the receiving table view (top, middle, bottom) for the row when scrolling concludes. See “Table View Scroll Position” a descriptions of valid constants.
animated
YES if you want to animate the change in position, NO if it should be immediate.

UITableView scrollToNearestSelectedRowAtScrollPosition example.
- (void) shrinkDidEnd:(NSString*) ident finished:(BOOL) finished contextInfo:(void*) nothing {
    NSIndexPath* sel = [searchResultsTable indexPathForSelectedRow];

    if (![[searchResultsTable indexPathsForVisibleRows] containsObject:sel])
    {
        [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

- (void) keyboardWillDisappear:(NSNotification*) n {
    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height += bounds.size.height; // add the keyboard height

    if (self.tabBarController != nil) {
        tableFrame.size.height -= 48; // subtract the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;   
    [UIView commitAnimations];

    [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

Example of [UITableView scrollToNearestSelectedRowAtScrollPosition].
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

[tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
static NSString *cellID = @\"celdaOK\";

Celda *celda = [tableView dequeueReusableCellWithIdentifier:cellID];

if (celda == nil){

NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@\"CeldaIMG\" owner:nil options:nil];

for(id currentObject in nibObjects)
{

if([currentObject isKindOfClass:[Celda class]])
{

celda = (Celda *)currentObject;

}

}

}

NSArray *arregloImagenes =
[NSArray arrayWithObjects:  [UIImage imageNamed:@\"animacionPrueba1.png\"],
[UIImage imageNamed:@\"animacionPrueba2.png\"],
[UIImage imageNamed:@\"animacionPrueba3.png\"],
[UIImage imageNamed:@\"animacionPrueba4.png\"],
[UIImage imageNamed:@\"animacionPrueba5.png\"],
[UIImage imageNamed:@\"animacionPrueba6.png\"], nil];


[[celda animacion] setAnimacion:arregloImagenes];

[[celda animacion] play];
NSLog(@\"aqui estoy\");

return celda;

}

UITableView scrollToNearestSelectedRowAtScrollPosition example.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedCellIndexPath = indexPath; 
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 
   
    return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionNone animated:YES];
}

End of UITableView scrollToNearestSelectedRowAtScrollPosition example article.