IOS-tableView中的cellHeadView隨着table滾動
設置table的style
首先要將table設置為UITableViewStyleGrouped類型。這樣就會得到tableView中的headView隨着table的滾動而滾動的現象了。
設置tableSection的高度
然后我們可以通過方法
tableView:heightForFooterInSection:
tableView:heightForHeaderInSection:
來控制tableSection中的header和Footer的高度。
這里需要注意一下,這里的高度如果設置為了0,那么函數是識別不出來的,一定要設置為一個小數,建議要設置為零的話,可以設置CGFLOAT_MIN。
下面是一個例子:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 10;//設置tableHeader的高度,10,可以識別
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section == 0) {
return CGFLOAT_MIN;//最小數,相當於0
}
else if(section == 1){
return CGFLOAT_MIN;//最小數,相當於0
}
return 0;//機器不可識別,然后自動返回默認高度
}
設置tableSection中的header和footer的view
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
view.backgroundColor = [UIColor redColor];
return view;
}