Search code examples
iphoneiosuitableviewuiscrollviewdelegate

Amount of scrolling in scrollViewWillBeginDragging


Is there any way to find how much UITableView has been scrolled in any direction ? I am interested in amount not in direction.


Solution

  • You can easily grab the exact offset of the table view by looking at its contentOffset property. For the vertical scroll, look at:

    tableView.contentOffset.y;
    

    and with this you can take your tableview to any particular location

    [theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];                        
    CGPoint point = theTableView.contentOffset;
    point .y -= theTableView.rowHeight;
    theTableView.contentOffset = point;
    

    for you requirement of load more cells after 10 you can use this logic

    - (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
        CGPoint offset = aScrollView.contentOffset;
        CGRect bounds = aScrollView.bounds;
        CGSize size = aScrollView.contentSize;
        UIEdgeInsets inset = aScrollView.contentInset;
        float y = offset.y + bounds.size.height - inset.bottom;
        float h = size.height;
        // NSLog(@"offset: %f", offset.y);   
        // NSLog(@"content.height: %f", size.height);   
        // NSLog(@"bounds.height: %f", bounds.size.height);   
        // NSLog(@"inset.top: %f", inset.top);   
        // NSLog(@"inset.bottom: %f", inset.bottom);   
        // NSLog(@"pos: %f of %f", y, h);
    
        float reload_distance = 10;
        if(y > h + reload_distance) {
            NSLog(@"load more rows");
        }
    }