在使用TableView中style設(shè)置成plain模式,在數(shù)據(jù)源代理中又有section分組就會出現(xiàn)如下的效果(每個section的頭視圖會有浮動效果,也稱headerview黏性 ),但如果我們不想讓他浮動(或有黏性),可以在scrollview的代理中實現(xiàn)如下代碼,即可實現(xiàn)我們想要的效果。

實現(xiàn)前.gif
override func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == self.tableView {
let sectionHeaderHeight = CGFloat(50)
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
}
}

實現(xiàn)后.gif
更多源碼請訪問github:https://github.com/zhangjiahuan8888