如何检测UITableView标题何时滚动到可见区域?

WPK*_*WPK 5 scroll uitableview ios

如何检测UITableView标头(表头,而不是节头)何时滚动到可见区域?

提前致谢!

ore*_*ren 10

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

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

的tableView:didEndDisplayingHeaderView:forSection:

但是,只有在方法中提供标题时才会调用此方法

的tableView:viewForHeaderInSection:

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

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

- (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;
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.