UITableView+UITableViewStyleGrouped 处理section之间间隙

[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]

这样初始化的tableview的样式是这样的:如下:

style设置为UITableViewStyleGrouped时header和footer不会悬浮在视图上,设置为UITableViewStylePlain就会悬浮。

然后我设置了每个section的高:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section==0){
//让第一个不显示
        return 0.1;}
    return 30;
}

-(UIView* )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return nil;
    }
    UILabel *lb=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    lb.text=[NSString stringWithFormat:@"%ld",section];
    lb.backgroundColor=[UIColor greenColor];
    return lb;
}


红框的区域是多出来的,我把header的高设置为30,header上的View高也是30;

但是会多出来,那片就是footer的区域,把他的高设置为0.1就行了(设置为0不行)

//header的高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section==0){
        return 0.1;}
    return 30;
}
//footer的高 必须在这设置footer的高不然tableview的尾部会多出一块
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==9) {
        return 0.1;
    }
    return 0.1;
}
//header的View
-(UIView* )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return nil;
    }
    UILabel *lb=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    lb.text=[NSString stringWithFormat:@"%ld",section];
    lb.backgroundColor=[UIColor greenColor];
    return lb;
}

或者在初始化tableview时加一句


//高设置为0.1可以;设置为0不行
self.tableview.tableFooterView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.1)];

 

 

 

转载于:https://my.oschina.net/lkeageer/blog/711805

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值