1. 程式人生 > >iOS UITabelView 頭檢視跟隨移動的設定

iOS UITabelView 頭檢視跟隨移動的設定

1.

在很多時候我們要給UITableView的頭檢視新增很多的樣式,並且每一個頭檢視的樣式還不一樣。並且要求頭檢視跟隨一起滾動過,例如APP的我的頁面,頭檢視跟隨移動,所以我們要在

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;中設定樣式

2.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;設定高度

在這個圖片中,我們分了三組,第一組的樣式養殖比較複雜,

if (section==0) {

header=[[UIViewalloc]init];

header.backgroundColor=[UIColorwhiteColor];

        [headeraddSubview:lineview];

self.myTableView.tableHeaderView=header;//讓我們自定義的UIVIew等於TableView.tableHeaderView

returnheader;

    }else{

UIView *view=[[UIViewalloc]init];

        view.backgroundColor

=[UIColorcolorWithHexString:@"#F2F3F5"];

return view;

 }

但是這樣是實現了頭檢視跟隨滾動,但是建立header的時候,我們切記不要設定座標,只需要初始化,剩下頭檢視的控制元件我們直接按照正常的放到header,放完控制元件後 我只需要在

//返回每組頭所對應的頭的高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

if (section==0) {

return0.8*SCREEN_WIDTH;

    }

return5;

}

如果我們在初始化的時候進行座標設定0.8*SCREEN_WIDTH的高度,我們會的頭檢視顯示會變成2倍的高度