如何检索所有可见的表节标题视图 [英] how to retrieve all visible table section header views

查看:80
本文介绍了如何检索所有可见的表节标题视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取所有可见的节标题视图?

is there a way to get all the section header views that are visible?

类似于UITableView的visibleCells实例方法.

something similar to UITableView's visibleCells instance method..

推荐答案

使用indexPathsForVisibleRows的问题是它不包括没有任何行的节.要获取所有可见部分,包括空白部分,您必须检查该部分的矩形并将其与表的contentOffset进行比较.

The problem with using indexPathsForVisibleRows is that it doesn't include sections without any rows. To get all visible section, including empty sections you have to check the rect of the section and compare it to the contentOffset of the table.

您还必须注意带浮动部分的纯样式和不带浮动部分的分组样式之间的区别.

You also have to pay attention to the difference between plain style with floating sections and grouped style without floating sections.

我创建了一个支持此计算的类别:

I made a category that support this calculation:

@interface UITableView (VisibleSections)

// Returns an array of NSNumbers of the current visible section indexes
- (NSArray *)indexesOfVisibleSections;
// Returns an array of UITableViewHeaderFooterView objects of the current visible section headers
- (NSArray *)visibleSections;

@end

@implementation UITableView (VisibleSections)

- (NSArray *)indexesOfVisibleSections {
    // Note: We can't just use indexPathsForVisibleRows, since it won't return index paths for empty sections.
    NSMutableArray *visibleSectionIndexes = [NSMutableArray arrayWithCapacity:self.numberOfSections];
    for (int i = 0; i < self.numberOfSections; i++) {
        CGRect headerRect;
        // In plain style, the section headers are floating on the top, so the section header is visible if any part of the section's rect is still visible.
        // In grouped style, the section headers are not floating, so the section header is only visible if it's actualy rect is visible.
        if (self.style == UITableViewStylePlain) {
            headerRect = [self rectForSection:i];
        } else {
            headerRect = [self rectForHeaderInSection:i];
        }
        // The "visible part" of the tableView is based on the content offset and the tableView's size.
        CGRect visiblePartOfTableView = CGRectMake(self.contentOffset.x, self.contentOffset.y, self.bounds.size.width, self.bounds.size.height);
        if (CGRectIntersectsRect(visiblePartOfTableView, headerRect)) {
            [visibleSectionIndexes addObject:@(i)];
        }
    }
    return visibleSectionIndexes;
}

- (NSArray *)visibleSections {
    NSMutableArray *visibleSects = [NSMutableArray arrayWithCapacity:self.numberOfSections];
    for (NSNumber *sectionIndex in self.indexesOfVisibleSections) {
        UITableViewHeaderFooterView *sectionHeader = [self headerViewForSection:sectionIndex.intValue];
        [visibleSects addObject:sectionHeader];
    }

    return visibleSects;
}

@end

这篇关于如何检索所有可见的表节标题视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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