1

可能重复:
减少 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;
}

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

4

1 回答 1

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];
}
于 2012-11-01T13:58:51.623 回答