很多情況下,我們需要給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;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}