您的位置:首页 > 产品设计 > UI/UE

UITableView的UITableViewStyleGrouped风格顶部空白问题

2016-11-17 14:18 399 查看
在使用UITableView的UITableViewStyleGrouped的属性时,会遇到每组的顶部空白如图1;

处理方法:设置UITableView的两个代理就可以解决:

// 解决方法:1(设置headView在代理方法中操作能避免空白)
-(UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section

{

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

    return 0.001;
}

//ps:项目需要headView的话就在上边代理中操作即可,如下

-(UIView
*)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section

{

   
switch (section) {

       
case
0:

           
return
_SectionOne;

           
break;

       
case
1:

           
return
_SectionTwo;

           
break;

       
case
2:

           
return
_SectionThree;

           
break;

           

       
default:

           
break;

    }

   
return [UIView
new];

}

-(CGFloat)tableView:(UITableView
*)tableView heightForHeaderInSection:(NSInteger)section

{

   
return
120.0;

}

_SectionOne
= [[UIImageView
alloc]
init];
_SectionOne.frame
=
CGRectMake(0,
0,
kScreenWidth,
200);
_SectionOne.backgroundColor
= [UIColor
grayColor];

// 解决方法:2

_tableView
= [[UITableView
alloc]
initWithFrame:CGRectMake(0,
0,
kScreenWidth,
kScreenHeight-64)
style:UITableViewStyleGrouped];
 _tableView.delegate
=
self;
 _tableView.dataSource
=
self;
 _tableView.sectionFooterHeight
=
0.0;//如果分组处理每组的foot还会有35像素的距离,加上此属性即可.
 _tableView.sectionHeaderHeight
=
0.0;//方法2:处理方法设置headView的高度为0即可。

[self.view
addSubview:_tableView];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息