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

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

以下是我的代码:

- (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];
        }
    }
}

Edit:

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

Edit 2:

以下是合并来自lnafzigerVadim 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:方法获取单元格的RECT,并使用CGRectContainsRect函数将其与表视图的边界RECT进行比较.

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

Swift

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

Obj-C

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

当然,这不会将表视图视为被超级视图裁剪或被另一个视图遮挡.

Ios相关问答推荐

Swift:从字符串目录获取带有参数的本地化字符串

缺少预期的键:';NSPrival yCollectedDataTypes';

在Android和iOS上从后台恢复应用程序后,inappwebview上出现白屏?

如何在SwiftUI ScrollView中zoom 中心项目?

SWIFT根据地球坐标修改 node 旋转

如何让3张组合的`SF Symbol`图片围绕一个特定点旋转?

Swift-如何通过Case Let访问模型变量

为什么下面的代码没有在主线程上运行?

SwiftUI - 搜索栏和导航标题之间的空间太大

当 .searchable 修饰符处于活动状态时,如何将变量设置为 false?

iOS 16.4 更新后 Xcode 14.3 构建错误:找不到 libarclite_iphoneos.a,链接器命令失败

如何为 XCTest、SwiftUI 预览等初始化带有 @MainActor 注释的 Swift 类

如何为视图的 colored颜色 和宽度设置动画?

如何识别 SwiftUI 中点击的按钮 ID 以使该按钮动画化?

将flutter android项目导入iOS

在 Swift 中上传带参数的图像

~= Swift 中的运算符

iOS7 UITextView contentsize.height 替代

快速从 NSTimeInterval 转换为小时、分钟、秒、毫秒

在 SKScene 中设置按钮