首页 > 技术文章 > IOS-tableView中的cellHeadView随着table滚动

AbeDay 2015-05-24 13:48 原文

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

推荐阅读