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

UITableView+UITableViewStyleGrouped 处理section之间间隙

2016-07-14 00:00 501 查看
摘要: 为了了解回顾uitableview,把今天遇到的一些小问题及处理方法裂了出来,加强一下记忆。

[[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)];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: