當(dāng) UITableView 的 style 屬性設(shè)置為 Plain 時硝拧,這個tableview的section header在滾動時會默認(rèn)懸停在界面頂端抱究。
#pragma mark - UIScrollViewDelegate
//使用這個方法主要是 讓tableview的section不要停留在界面上
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 30;
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);
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
CGFloat sectionHeaderHeight = 30;
if (scrollView.contentInset.top < 0 &&
scrollView.contentInset.top < fabs(sectionHeaderHeight) &&
scrollView.contentOffset.y <= sectionHeaderHeight+0.5) {
[UIView animateWithDuration:0.3f animations:^{
[scrollView setContentOffset:(CGPoint){0,0}];
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
CGFloat sectionHeaderHeight = 30;
if (scrollView.contentInset.top < 0 &&
scrollView.contentInset.top < fabs(sectionHeaderHeight) &&
scrollView.contentOffset.y <= sectionHeaderHeight+0.5) {
[UIView animateWithDuration:0.3f animations:^{
[scrollView setContentOffset:(CGPoint){0,0}];
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}];
}
}