How can I detect when the UITableView title is scrolled out of the viewable area?

How can I detect when the UITableView title (table title, not section title) scrolls out of the viewable area?

Thanks in advance!

+3


source to share


2 answers


There are several possible solutions I can think of:

1) You can use this delegate method:

Tableview: didEndDisplayingHeaderView: forSection:

However, this method is only called if you provide a header in the method

Tableview: viewForHeaderInSection:



You said "not a section header", but you can use the first section header in a grouped tableView as a headerView table. (Grouped for title will scroll along with the table view)

2) If you don't want to use the grouped tableView and section header, you can use the scrollView delegate (UITableViewDelegate corresponds to UIScrollViewDelegate). Just check if the scrolling of the tableView is enough to make the tableHeaderView disappear. See the following code:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    static CGFloat lastY = 0;

    CGFloat currentY = scrollView.contentOffset.y;
    CGFloat headerHeight = self.headerView.frame.size.height;

    if ((lastY <= headerHeight) && (currentY > headerHeight)) {
        NSLog(@" ******* Header view just disappeared");
    }

    if ((lastY > headerHeight) && (currentY <= headerHeight)) {
        NSLog(@" ******* Header view just appeared");
    }

    lastY = currentY;
}

      

Hope this helps.

+6


source


This is how a tableView can indicate whether it is tableViewHeader

visible or not (Swift 3):



extension UITableView{

    var isTableHeaderViewVisible: Bool {
        guard let tableHeaderView = tableHeaderView else {
            return false
        }

        let currentYOffset = self.contentOffset.y;
        let headerHeight = tableHeaderView.frame.size.height;

        return currentYOffset < headerHeight
    }

}

      

+1


source







All Articles