Search code examples
iphoneuiscrollviewdelegate

UITableView/UIScrollView scrollToItemAtIndex


I'm trying to call

[tableView scrollToItemAtIndex: [tableView indexForCell: firstVisibleCell] atScrollPosition: UITableViewScrollPositionTop animated:YES];

To force my table to 'snap' to the first visible item. This works fine, but I only get a call to

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView

When scrolling had to be completed (which is stated in the document). Is there anyway that I can get the offset of the firstVisibleCell so I can do a comparison to see if scrolling is required?


Solution

  • Use the method - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath:

    NSIndexPath *indexPath = [tableView indexForCell:firstVisibleCell];
    CGRect rect = [tableView rectForRowAtIndexPath:indexPath];