iXcoder iXcoder - 3 months ago 15
iOS Question

How to get a UITableView's visible sections?

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

Answer

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.