【问题标题】:How to detect when a UITableView header is scrolled off visible area?如何检测 UITableView 标头何时从可见区域滚动?
【发布时间】:2015-01-09 17:59:33
【问题描述】:

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

提前致谢!

【问题讨论】:

    标签: ios uitableview scroll


    【解决方案1】:

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

    1) 你可以使用这个委托的方法:

    tableView:didEndDisplayingHeaderView:forSection:

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

    tableView:viewForHeaderInSection:

    您说“不是节标题”,但您可以将分组 tableView 中的第一个节标题用作表 headerView。 (分组是为了表头会和表格视图一起滚动)

    2) 如果不想使用分组的tableView和section header,可以使用scrollView的delegate(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;
    }
    

    希望对您有所帮助。

    【讨论】:

    • 好吧,代码是正确的,但是没有理由设置 lastY 值,因为在每次调用中都会再次设置它...
    • @Skodik.o 你能解释一下你的意思吗? lastY 是要知道上次滚动视图在哪里,没有它,它会在每次滚动不可见时打印“标题视图刚刚消失”,您需要在标题出现/消失时准确打印它。它是静态的,仅第一次设置为 0...
    • tableView:didEndDisplayingHeaderView:forSection: 为我做,谢谢。
    【解决方案2】:

    以下是 tableView 如何指定其 tableViewHeader 是否可见(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
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多