UITableView各部分之间的iOS空间不受控制?

时间:2012-11-01 12:28:54

标签: objective-c ios uitableview

  

可能重复:
  Reducing the space between sections of the UITableView

我正在使用UITableView分组样式,我想减少各个部分之间的间距。我试过这些:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 32)];

    UIImageView* headerbg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_bg.png"]];
    headerbg.frame = CGRectMake(8.0, 0.0, 302.0, 44.0);
    [customView addSubview:headerbg];

    UILabel * headerLabel = [[FontLabel alloc] initWithFrame:CGRectZero fontName:@"HelveticaNeue-Medium"
                                               pointSize:16.0f];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor colorWithRed:17.0/255.0 green:117.0/255.0 blue:1.0/255.0 alpha:1.0];
    headerLabel.frame = CGRectMake(10, 10.0, 300.0, 30);
    headerLabel.text = @"Reserve";

    [headerbg addSubview:headerLabel];

    if(section == 1 || section == 2)
    {
        headerbg.image = nil;
        headerLabel.text = nil;
        return nil;
    }
    return customView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

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

        return 25;
    }
    else
        return 0;

}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 0;
}

但是我在各个部分之间获得了更多的空间。 我错过了什么?

1 个答案:

答案 0 :(得分:4)

只需查看代码,我已经在代码中替换了一行:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0) 
    return 25;
else
    return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
 UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 32)];

UIImageView* headerbg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top_bg.png"]];
headerbg.frame = CGRectMake(8.0, 0.0, 302.0, 44.0);
[customView addSubview:headerbg];

UILabel * headerLabel = [[FontLabel alloc] initWithFrame:CGRectZero fontName:@"HelveticaNeue-Medium"
                                           pointSize:16.0f];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor colorWithRed:17.0/255.0 green:117.0/255.0 blue:1.0/255.0 alpha:1.0];
headerLabel.frame = CGRectMake(10, 10.0, 300.0, 30);
headerLabel.text = @"Reserve";

[headerbg addSubview:headerLabel];

if(section == 1 || section == 2)
{
    headerbg.image = nil;
    headerLabel.text = nil;
    return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}
return customView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}