Thursday, 9 January 2014

How to focus on last cell in a UITableview in Xcode



//Create a method to find out the last index of your table view and will focus to that cell with an animation:

-(void)autoFocusTableCell{
    NSIndexPath *lastIndexPath = [self lastIndexPath];
    [myTableView scrollToRowAtIndexPath:lastIndexPath
                       atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}


//Code to find the last index of your tableview.
-(NSIndexPath *)lastIndexPath{
    NSInteger lastSectionIndex = MAX(0, [myTableView numberOfSections] - 1);
    NSInteger lastRowIndex = MAX(0, [myTableView numberOfRowsInSection:lastSectionIndex] - 1);
    return [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
}

//Then call the method
    [self autoFocusTableCell];