且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测何时将UITableView标头滚动到可见区域之外?

更新时间:2023-02-05 23:25:03

我可以想到几种可能的解决方案:

There are couple of possible solutions I can think of:

1) 您可以使用此委托的方法:

1) You can use this delegate's method:

tableView:didEndDisplayingHeaderView:forSection:

tableView:didEndDisplayingHeaderView:forSection:

但是,仅当您在方法中提供标头时,才会调用此方法

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

tableView:viewForHeaderInSection:

tableView:viewForHeaderInSection:

您说的是不是节标题",但是您可以将分组的tableView中的第一个节标题用作表headerView. (分组的标题将与表格视图一起滚动)

You said 'not section header', but you can use the first section header in a grouped tableView as the table headerView. (The grouped is for the header will scroll together with the table view)

2) 如果您不想使用分组的tableView和section标头,则可以使用scrollView的委托(UITableViewDelegate符合UIScrollViewDelegate).只需检查tableView何时滚动足以使tableHeaderView消失即可.请参见以下代码:

2) If you don't want to use grouped tableView and the section header, you can use the scrollView's delegate (UITableViewDelegate conforms to UIScrollViewDelegate). Just check when the tableView is scrolled enough for disappearing the tableHeaderView. 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;
}

希望它会有所帮助.