1

有什么方法可以找到 UITableView 在任何方向上滚动了多少?我感兴趣的是数量而不是方向。

4

3 回答 3

2

您可以通过查看其 contentOffset 属性轻松获取 table view 的确切偏移量。对于垂直滚动,请查看:

tableView.contentOffset.y;

有了这个,你可以把你的 tableview 带到任何特定的位置

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

对于您需要在 10 之后加载更多单元格,您可以使用此逻辑

- (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");
    }
}
于 2013-09-06T10:00:16.463 回答
1

您需要测量拖动开始和结束contentOffset的时间。UITableView取两者之间的差异,它将为您提供从初始位置到最终位置的变化量。

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
}

希望有帮助!

于 2013-09-06T09:56:21.417 回答
0

您可以使用以下语法找到它...

NSLog(@"scrolled:%f",yourtableview.contentOffset.y);//y for vertical, x for horizontal
于 2013-09-06T09:48:37.077 回答