iOS關(guān)于實(shí)現(xiàn)UITableView組頭組尾視圖/標(biāo)題不懸停一起滾動(dòng)的方法

在實(shí)際開發(fā)中,我們經(jīng)常會(huì)遇見一些需求是TableView的cell結(jié)合section組頭組尾的視圖一起滾動(dòng)的需求,比如下面需求,如下圖以及我分析的圖:

image.png

[圖片上傳中...(image.png-d3f328-1604105256439-0)]
tableview style不同的區(qū)別
1、style=UITableViewStyleGrouped 默認(rèn)會(huì)有headview和footview,頭尾會(huì)空出一些距離,headview和footview會(huì)隨tableview一起滑動(dòng)。而在tableview的代理方法:返回組的頭/尾視圖中設(shè)置具體高度時(shí),開頭結(jié)尾總是默認(rèn)有一段距離,并且如果設(shè)置她們中的某個(gè)距離為0,則無效。
正確的處理方法:
1)設(shè)置標(biāo)頭的高度為特小值 (不能為零 為零的話蘋果會(huì)取默認(rèn)值就無法消除頭部間距了)

   UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
   view.backgroundColor = [UIColor redColor];
   self.tableView.tableHeaderView = view;

2)寫代理方法(中間的留白其實(shí)是段尾的高度 代理的作用設(shè)置段尾的高度 返回值也不能為0(設(shè)置為0 在ios看來等于未設(shè)置)

   -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
        return 0.01f; 
       或者
       return CGFLOAT_MIN;
    }

2、style=UITableViewStylePlain 默認(rèn)沒有headview和footview,自定義的headview和footview不會(huì)隨tableview一起滑動(dòng):
特點(diǎn):
1)有多段時(shí)(區(qū)頭,區(qū)尾), 段頭停留(自帶粘性效果)
2)沒有中間的間距和頭部間距(要想有的重寫UITableViewCell \UITableViewHeaderFooterView里面的setFrame方法)

上面介紹完兩種樣式的特定,那么就要注意創(chuàng)建tableview時(shí)候的樣式,以及說說實(shí)現(xiàn)組頭組尾不懸停的方法:

[[UITableView alloc]init];

以及直接把tableview拖入到xib或者storyboard里面的時(shí)候默認(rèn)都是UITableViewStylePlain樣式,從蘋果源碼里面就可以看到,如下圖:

image.png

這個(gè)時(shí)候如果還想組頭和組尾一起滾動(dòng),目前最常見的方法就是:

- (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);
    }
}

這里的sectionHeaderHeight就是指組頭的高度,組尾的方法也有,不過個(gè)人好像還沒發(fā)現(xiàn)其它方式,如果UITableViewStylePlain樣式下還有有其它讓組頭組尾一起滾動(dòng)的方式,歡迎和我交流。

2、采用

[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

方法創(chuàng)建UITableView,這個(gè)時(shí)候就指定為了UITableViewStyleGrouped樣式,這個(gè)時(shí)候不用增加其它方法,直接使用tableview的代理方法:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    XMFOrdersHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMFOrdersHeaderView class]) owner:nil options:nil] firstObject];;
    
    headerView.orderModel = self.dataSourceArr[section];
    
    
    return headerView;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 58;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    XMFOrdersFooterView *footerView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMFOrdersFooterView class]) owner:nil options:nil] firstObject];;
    
    footerView.orderModel = self.dataSourceArr[section];
    
    footerView.sectionNum = section;
    
    footerView.delegate = self;
    
    
    return footerView;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 58;
    
}

3、說說用一個(gè)底層方法實(shí)現(xiàn)不懸停

//導(dǎo)入頭文件
#import <objc/message.h>
//在加載視圖的方法里寫如下代碼  (將里面的_tableView換成自己的表視圖)
((void (*) (id,SEL,BOOL)) objc_msgSend) (_tableView,NSSelectorFromString(@"_setHeaderAndFooterViewsFloat:"),NO);

到此為止,即可很好地實(shí)現(xiàn)組頭組尾的設(shè)置以及跟隨tableview一起滾動(dòng)了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容