[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped]
这样初始化的tableview的样式是这样的:以下:code
style设置为UITableViewStyleGrouped时header和footer不会悬浮在视图上,设置为UITableViewStylePlain就会悬浮。orm
而后我设置了每一个section的高:string
-(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;it
可是会多出来,那片就是footer的区域,把他的高设置为0.1就好了(设置为0不行)io
//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时加一句table
//高设置为0.1能够;设置为0不行 self.tableview.tableFooterView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.1)];