Frage

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

War es hilfreich?

Lösung

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");
    }
}

Andere Tipps

You need to measure the contentOffset of the UITableView when dragging begins and when it ends. Take a difference between the two, it will give you the amount of change from initial to final position.

CGPoint oldOffset;
CGPoint newOffset;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
      oldOffset = scrollView.contentOffset;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
      newOffset = *targetContentOffset;
      CGPoint diff = {newOffset.x - oldOffset.x, newOffset.y - oldOffset.y};

      // Where diff.x => amount of change in x-coord of offset
      // diff.y => amount of change in y coord of offset
}

Hope that helps!

you can find it using below syntax...

NSLog(@"scrolled:%f",yourtableview.contentOffset.y);//y for vertical, x for horizontal
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top