網(wǎng)上解決這個(gè)問題大致有兩種方法:
- 將 tableView 的 style 設(shè)置為 UITableViewStyleGrouped
- 將 headerView 賦值給 tableHeaderView 而不是 sectionHeaderVidw
但這兩種方法有時(shí)候并不好用窘拯。所以,為了實(shí)現(xiàn)分組效果躲株,但是又不希望分組的標(biāo)簽欄(SectionHeaderView或者SectionFooterView)懸停片部,還有沒有其他的實(shí)現(xiàn)方式呢?
比較好用的一種方法:
網(wǎng)上還有一種重寫ScrollView代理函數(shù)的方法霜定,這個(gè)方法比較好用档悠,原理是將tableView的content不斷上移,逐漸隱藏在導(dǎo)航欄下望浩,其實(shí)它依然是懸停的辖所,只不過我們看不到罷了。
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat sectionHeaderHeight = section的高度;
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);
}
}
}
但是據(jù)說添加了下拉刷新的話就會(huì)有bug了磨德,因?yàn)楹芏嘞吕⑿乱彩峭ㄟ^設(shè)置contentInset實(shí)現(xiàn)的缘回,所以有下拉刷新的可能會(huì)有一定問題,沒有試典挑,具體效果不詳
還有一種跟上面比較類似的方法酥宴,是重寫headSectionView的setframe方法,供參考
-(void)setFrame:(CGRect)frame{
CGRect sectionRect = [self.tableView rectForSection:self.section];
CGRect newFrame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(sectionRect), CGRectGetWidth(frame), CGRectGetHeight(frame));
[super setFrame:newFrame];
}
這里給大家介紹另一個(gè)思路:
也許這個(gè)辦法很笨拙您觉,但是在一些走投無路場(chǎng)合也許就會(huì)幫到你拙寡。
如果我們把標(biāo)簽這一欄作為一個(gè)特殊的分組是不是可以解決這個(gè)問題呢?答案是肯定的顾犹。如果按照這個(gè)思路來做倒庵,那么接下來要做的就是在tableView的代理函數(shù)中做一些判斷和修改。
假設(shè)我們有兩個(gè)分組的數(shù)據(jù)要展示炫刷,并且每個(gè)分組前的標(biāo)簽要跟隨cell一起滾動(dòng)擎宝。
原本應(yīng)該是這樣的
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
但這時(shí)則要return 4;
,因?yàn)槲覀儼褍蓚€(gè)標(biāo)簽欄也作為了一個(gè)分組浑玛。
同樣
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch (section) {
case 0: return 1; break;
case 1: return _section1DataArr.count; break;
case 2: return 1; break;
case 3: return _section2DataArr.count; break;
default: return 0; break;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section) {
case 0: return 50; break;
case 1: return 40; break;
case 2: return 50; break;
case 3: return 40; break;
default: return 0; break;
}
}
相信你已經(jīng)明白如何實(shí)現(xiàn)了绍申,其他的代理函數(shù)中的實(shí)現(xiàn)就不贅述了。
效果如下:
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 ,轉(zhuǎn)載時(shí)必須注明出處及相應(yīng)鏈接极阅!