检查 UITableViewCell 是否完全可见的最佳方法 [英] Best way to check if UITableViewCell is completely visible

查看:26
本文介绍了检查 UITableViewCell 是否完全可见的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含不同高度单元格的 UITableView,我需要知道它们何时完全可见.

I have a UITableView with cells of different heights and I need to know when they are completely visible or not.

目前我正在循环查看可见单元格列表中的每个单元格,以检查每次滚动视图时它是否完全可见.这是最好的方法吗?

At the moment I am looping through each cell in the list of visible cells to check if it is completely visible every time the view is scrolled . Is this the best approach?

这是我的代码:

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {

    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;    
    NSArray* cells = myTableView.visibleCells;

    for (MyCustomUITableViewCell* cell in cells) {

        if (cell.frame.origin.y > offset.y &&
            cell.frame.origin.y + cell.frame.size.height < offset.y + bounds.size.height) {

            [cell notifyCompletelyVisible];
        }
        else {

            [cell notifyNotCompletelyVisible];
        }
    }
}

请注意 *- (NSArray )visibleCells 返回完全可见和部分可见的可见单元格.

Please note that *- (NSArray )visibleCells returns visible cells which are both completely visible and partly visible.

编辑 2:

这是结合 lnafzigerVadim Yelagin 的解决方案后修改后的代码:

This is the revised code after combining solutions from both lnafziger and Vadim Yelagin:

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    NSArray* cells = myTableView.visibleCells;
    NSArray* indexPaths = myTableView.indexPathsForVisibleRows;

    NSUInteger cellCount = [cells count];

    if (cellCount == 0) return;

    // Check the visibility of the first cell
    [self checkVisibilityOfCell:[cells objectAtIndex:0] forIndexPath:[indexPaths objectAtIndex:0]];

    if (cellCount == 1) return;

    // Check the visibility of the last cell
    [self checkVisibilityOfCell:[cells lastObject] forIndexPath:[indexPaths lastObject]];

    if (cellCount == 2) return;

    // All of the rest of the cells are visible: Loop through the 2nd through n-1 cells
    for (NSUInteger i = 1; i < cellCount - 1; i++)
        [[cells objectAtIndex:i] notifyCellVisibleWithIsCompletelyVisible:YES];
}

- (void)checkVisibilityOfCell:(MultiQuestionTableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
    CGRect cellRect = [myTableView rectForRowAtIndexPath:indexPath];
    cellRect = [myTableView convertRect:cellRect toView:myTableView.superview];
    BOOL completelyVisible = CGRectContainsRect(myTableView.frame, cellRect);

    [cell notifyCellVisibleWithIsCompletelyVisible:completelyVisible];
}

推荐答案

您可以使用 rectForRowAtIndexPath: 方法并使用 CGRectContainsRect 功能.

You can get the rect of a cell with rectForRowAtIndexPath: method and compare it with tableview's bounds rect using CGRectContainsRect function.

请注意,如果它不可见,这将不会实例化单元格,因此会相当快.

Note that this will not instantiate the cell if it is not visible, and thus will be rather fast.

迅捷

let cellRect = tableView.rectForRowAtIndexPath(indexPath)
let completelyVisible = tableView.bounds.contains(cellRect)

Obj-C

CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
BOOL completelyVisible = CGRectContainsRect(tableView.bounds, cellRect);

当然,这不会考虑 table view 被 superview 裁剪或被另一个 view 遮挡.

Of course this will not regard the table view being clipped by a superview or obscured by another view.

这篇关于检查 UITableViewCell 是否完全可见的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆