当节没有行时,确定UITableView的可见部分

时间:2014-04-04 20:03:45

标签: ios objective-c uitableview

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

3 个答案:

答案 0 :(得分:3)

我能想到的最好的方法是使用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;
}

答案 1 :(得分:1)

Swift版

if let visibleRows = tableView.indexPathsForVisibleRows {
    let visibleSections = visibleRows.map({$0.section})
}

答案 2 :(得分:0)

Swift 3版本@David回答

func visibleSection() -> IndexSet {

        var indexSet: IndexSet = IndexSet()
        let visibleRect: CGRect = self.bounds

        for sectionIndex in 0..<self.numberOfSections {

            let sectionBounds = self.rect(forSection: sectionIndex)

            if sectionBounds.intersects(visibleRect) {

                indexSet.insert(sectionIndex)
            }
        }
        return indexSet
    }
相关问题