場景:
當 UITableView
的 style
屬性設置為 Plain
時
tableview
的 section header
在滾動到界面頂端時
會 懸停
!
疑問:
1.如何在不使用Grouped
時凸丸,讓組頭不懸停蚀之?斗搞?
2.如何在不重寫-scrollViewDidScroll:
(示例) 方法時力喷,讓組頭不懸停?眷昆?砸捏?
辦法:
1.針對沒有內(nèi)容的 section header
直接設置 header view
的背景顏色為 clearColor
設置 tableview
的背景顏色為 section header
的背景顏色
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(0, 0, kScreenWidth, 10);
view.backgroundColor = [UIColor clearColor];
return view;
}
2.針對有內(nèi)容的 section header
- 使用
Grouped
- 參考 示例
示例無法查看,請看以下原文(來自:http://www.reibang.com/p/333f361ada36)
當 UITableView 的 style 屬性設置為 Plain 時隙赁,這個tableview的section header在滾動時會默認懸停在界面頂端。取消這一特性的方法有兩種:
1梆暖、將 style 設置為 Grouped 伞访。這時所有的section header都會隨著scrollview滾動了。不過 grouped 和 plain 的樣式有輕微區(qū)別轰驳,切換樣式后也許需要重新調(diào)整UI
2厚掷、重載scrollview的delegate方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 40;
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);
}
}