OGeek|极客世界-中国程序员成长平台

标题: ios - 当部分没有行时确定 UITableView 的可见部分 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 07:26
标题: ios - 当部分没有行时确定 UITableView 的可见部分

我正在尝试确定哪些部分当前在我的 UITableView 中可见。但是,有时我的部分没有行,只显示它们的部分标题。有没有办法确定,也许通过使用节标题,当前正在显示我的 UITableView 中的哪些节?询问 -(NSArray *)indexPathsForVisibleRows 无法确定该部分,因为没有实际可见的行,只有部分标题。



Best Answer-推荐答案


我能想到的最好办法是使用 contentOffset 和 frame.size 来计算可见矩形并遍历调用 rectForSection: 的每个部分并查看哪些部分相交。像这样的东西:

-(NSIndexSet*)visibleSections
{
    NSMutableIndexSet*  indexSet = [NSMutableIndexSet new];
    CGRect              visible = self.tableView.bounds;

    for(NSInteger section = 0 ; section < [self numberOfSections])
    {
        CGRect          sectBounds = [self.tableView rectForSection:section];

        if(CGRectIntersectsRect(sectBounds, visible))
        {
            [indexSet addIndex:section];
        }
    }

    return indexSet;
}

关于ios - 当部分没有行时确定 UITableView 的可见部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22872042/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4