How to get a UITableView's visible sections?

前端 未结 12 1789
轻奢々
轻奢々 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:10

    I have got the solution.

    First step, each section will show a UIView that created by - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section, that will be stored into array.

    When the TableView is scrolled , I want free the invisible section view, so I need know which section is visible or not, follow function code will detect for this purpose, if the view is visible then free it.

    -(BOOL)isVisibleRect:(CGRect)rect containerView:(UIScrollView*)containerView
    {
        CGPoint point = containerView.contentOffset;
        CGFloat zy = point.y ;
    
        CGFloat  py =  rect.origin.y + rect.size.height;
        if (py - zy <0) {
                return FALSE;
        }
        CGRect  screenRect = containerView.frame;
    
        CGFloat by = screenRect.size.height + zy ;
        if (rect.origin.y > by) {
                return FALSE;
        }
        return TRUE;
    }
    

    (rect is the frame of the section UIView; containerView is the UITableView)

    In this way, I can get visible sections of the UITableView, but I hope the SDK can provide API for this purpose directly.

提交回复
热议问题