2017-01-16 32 views
0

我想要检测何时UITableView部分标题捕捉到屏幕顶部,然后修改标题的高度,但我不知道该怎么做。谁能帮忙?当UITableView部分标题捕捉到屏幕顶部时检测到

+0

我从来没有尝试过,但也许你可以尝试'frame.x == 0'? –

+2

有一个'rectForHeader'方法,您可以使用它来获取该部分的头部框架,然后是'UItableView'的'contentOffset'属性。我相信你可以计算一下,看看头是否在屏幕顶部。还有'UIScrollViewdelegate'方法'scrollViewDidScroll',你可以用它来检查'UITableView'何时滚动 –

+0

非常感谢 –

回答

1

我去让我的评论到代码和它的工作原理是:

override func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    if tableView.rectForHeader(inSection: 0).origin.y <= tableView.contentOffset.y-defaultOffSet.y && 
     tableView.rectForHeader(inSection: 1).origin.y > tableView.contentOffset.y-defaultOffSet.y { 
     print("Section Header I want is at top") 
    } 
} 

所以,你有Int当该节的头是在顶部,该print将run.Note是tableView是不能代替sectionIWant一个参数scrollViewDidScroll,所以你必须在其他地方保留对你的UITableView的引用,并在这里使用它。

另外,如果你想不断更新上一节标头是在顶部,你可以使用:

override func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    var currentSection = 0 
    while !(tableView.rectForHeader(inSection: currentSection).origin.y <= (tableView.contentOffset.y-defaultOffSet.y) && 
     tableView.rectForHeader(inSection: currentSection+1).origin.y > (tableView.contentOffset.y)-defaultOffSet.y) { 
      if tableView.contentOffset.y <= tableView.rectForHeader(inSection: 0).origin.y { 
       break //Handle tableView header - if any 
      } 
      currentSection+=1 
      if currentSection > tableView.numberOfSections-2 { 
       break //Handle last section 
      } 
    } 
    print(currentSection) 
} 

注意,在这两种代码有一个defaultOffSet这是tableView负载的contentOffSet,这是考虑到contentOffSet在某些情况下不从0开始 - 我不确定何时但我确实遇到过这种情况。

相关问题