计算UITableView在哪一行停止滚动?

发布于 2024-11-09 08:55:00 字数 109 浏览 3 评论 0原文

当表格有很多行时,用户可以向上/向下滑动表格。这将创建一个滚动动画,该动画似乎具有确定的长度,具体取决于轻弹手势的速度/长度。如果没有进一步的用户交互,是否可以可靠地计算一旦滚动停止,表中的哪些行将可见?

When a table has many rows, the user can flick up/down the table. This creates a scrolling animation which seems to have a deterministic length depending on the speed/length of the flick gesture. Is it possible to reliably calculate what rows in the table will be visible once the scrolling stops if there is no further user interaction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半边脸i 2024-11-16 08:55:00

在 iOS 5.0 及更高版本中,有一个针对 UIScrollViewDelegate 称为 scrollViewWillEndDragging:withVelocity:targetContentOffset:。使用这种新方法,可以计算甚至修改滚动的停止位置,如视频会话 100:新增内容中所述在 Cocoa Touch 中,大约第九分钟。

In iOS 5.0 and later there is a new API for the UIScrollViewDelegate called scrollViewWillEndDragging:withVelocity:targetContentOffset:. With this new method the stop position of the scroll can be calculated and even modified, as explained in the Video Session 100: What's New in Cocoa Touch, around the ninth minute.

披肩女神 2024-11-16 08:55:00

UITableView 继承自 UIScrollView,您可以通过使用 UIScrollViewDelegate 方法和表视图 indexPathsForVisibleRows 属性来检查来完成此操作滚动停止时哪些单元格索引路径可见。

您甚至可以保存从减速开始的初始位置,以便您可以计算滚动方向是向上还是向下,然后让您知道它将停止的单元格是可见的第一个还是最后一个那些。

int startDeceleratingPosition;

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

    startDeceleratingPosition = scrollView.contentOffset.y;

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    BOOL isPositionUp = startDeceleratingPosition < scrollView.contentOffset.y;     

    NSArray *paths = [_myTableview indexPathsForVisibleRows];
    UITableViewCell *cell;
    if(isPositionUp){
        cell = [_myTableview cellForRowAtIndexPath:[paths objectAtIndex:0]];
    } else {
        cell = [_myTableview cellForRowAtIndexPath:[paths lastObject]];
    }

}

关于上面代码的一个重要注意事项是,它将表格视图作为变量 _myTableview 指向,而不是仅仅将委托方法变量 scrollView 转换为 UITableView *< /code>,尽管这只是实现细节,不应该影响这里的逻辑。

UITableView inherits from UIScrollView and you can accomplish that by using the UIScrollViewDelegate methods and the table view indexPathsForVisibleRows property to check which cell index paths are visible at the moment the scrolling stops.

You can even save the initial position from where the deceleration started, so that you can calculate whether the scroll direction was up or down, which will then let you know if is the cell that it will stop is the first or the last of the visible ones.

int startDeceleratingPosition;

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

    startDeceleratingPosition = scrollView.contentOffset.y;

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    BOOL isPositionUp = startDeceleratingPosition < scrollView.contentOffset.y;     

    NSArray *paths = [_myTableview indexPathsForVisibleRows];
    UITableViewCell *cell;
    if(isPositionUp){
        cell = [_myTableview cellForRowAtIndexPath:[paths objectAtIndex:0]];
    } else {
        cell = [_myTableview cellForRowAtIndexPath:[paths lastObject]];
    }

}

An important note about the code above is that it points to the table view as a variable _myTableview instead of just casting the delegate method variable scrollView to a UITableView *, although that is just implementation details and should not affect the logic here.

多情癖 2024-11-16 08:55:00

有趣的问题...... UITableViewDelegate 也符合 UIScrollViewDelegatehttp://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intf/UIScrollViewDelegate

有您可以使用一些委托回调来了解滚动何时开始减速以及何时结束减速。

您可能可以使用 –scrollViewDidEndDecelerating:,然后使用 tableView(tableView 子类 UIScrollView)的单元格高度和内容偏移属性,然后计算可见的单元格减速后。

Interesting question..... UITableViewDelegate conforms to UIScrollViewDelegate as well: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intf/UIScrollViewDelegate

There are some delegate callbacks you could use to know when the scrolling begins to decelerate, and ends decelerating.

You could probably use – scrollViewDidEndDecelerating:and at that point use the cell heights and the content offset property of the tableView (tableView subclasses UIScrollView) and then calculate the cells that are visible after deceleration.

辞取 2024-11-16 08:55:00

我不知道如何确定将显示多少行,但您始终可以获得已显示的行数。 (一旦桌子停止,不再接触。)
不确定这是否有帮助,但这就是你要做的

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    // make sure to declare your integer in your .h file and to also synthesize
    //say this is your int "howManyRowsAreShowing"
    howManyRowsAreShowing = indexPath.Row;



    //the rest of the code below is generic table view code for example only
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
    cell.text = cellValue;

    return cell;
    }

I dont know how you could determine how many rows WILL show, but you can always get how many rows HAVE shown. (once the table stops w/no further touching.)
Not sure if that helps but this is how you would do it

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    // make sure to declare your integer in your .h file and to also synthesize
    //say this is your int "howManyRowsAreShowing"
    howManyRowsAreShowing = indexPath.Row;



    //the rest of the code below is generic table view code for example only
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
    cell.text = cellValue;

    return cell;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文