How to get a UITableView's visible sections?

前端 未结 12 1782
轻奢々
轻奢々 2020-12-14 19:24

UITableView provides the methods indexPathsForVisibleRows and visibleCells, but how can I get the visible sections?

12条回答
  •  攒了一身酷
    2020-12-14 20:14

    another solution, use 1 bit in your section header view's tag, like that

    #define _TBL_TAG_SECTION(_TAG) ((_TAG)|(1<<30))
    #define _TBL_TAG_CLEAR(_TAG) ((_TAG)&((1<<30)-1))
    #define _TBL_TAG_IS_SECTION(_TAG) ((_TAG)>>30)
    
    - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        // alloc header view
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
        header.tag = _TBL_TAG_SECTION(section);
        return header;
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGRect r = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y,
                          CGRectGetWidth(scrollView.frame),
                          CGRectGetHeight(scrollView.frame));
        for (UIView *v in [_tableView subviews]) {
            if ( CGRectIntersectsRect(r, v.frame) ) {
                if ( _TBL_TAG_IS_SECTION(v.tag) ) {
                    NSLog(@"visible section tag %d", _TBL_TAG_CLEAR(v.tag));
                }
            }
        }
    }
    

提交回复
热议问题