UITableView的段頭設置

很多情況下,我們需要給UITableView設置段頭,段頭一般分爲兩種情況:

1、無分組情況下(UITableViewStylePlain),只需要實現這兩個代理方法,即可設置頭部,只不過,這樣設置的頭部留在最頂層,不隨tableView滑動而滑動

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

// 返回段頭的View

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENSIZE.width, 35)];
    view.backgroundColor = [Tools getColor:@"FAFAFA"];
    _timerLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 5, SCREENSIZE.width, 25)];
    _timerLabel.textColor = [UIColor grayColor];
    _timerLabel.font = [UIFont systemFontOfSize:14];
    [view addSubview:_timerLabel];
    return view;
}

這種樣式下,增加此方法,會有段頭,有分組,並且段頭滑動到頂部,會停留在頂部

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

2、分組樣式下  UITableViewStyleGrouped

這種樣式下,增加此方法,會有段頭,有分組,只不過設置的頭部隨tableView滑動而滑動,不在頂部停留

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}




發佈了19 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章