基于标题文本高度的UITableView部分的页眉/页脚高度

DBo*_*yer 8 xcode objective-c uitableview ios

我想知道是否可以使UITableView的一部分的高度页眉/页脚等于页眉/页脚标题文本的高度.任何提示都会很棒!

注意:我的TableView的某些部分可能没有页眉/页脚,在这种情况下,只有部分之间有填充,因为在这种情况下"headerTitleHeight/footerTitleHeight"将为零.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return headerTitleHeight + padding;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return footerTitleHeight + padding;
}
Run Code Online (Sandbox Code Playgroud)

Xei*_*han 12

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

    NSString *myHeader = [sectionsArray objectAtIndex:section];
    CGSize maxSize = CGSizeMake(320, 999999.0);
    int height = 0;

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];

    if (IS_IOS_6) //Macro i made for if iOS 6
    {
        CGSize size = [myHeader sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:(15)]
                             constrainedToSize:maxSize
                                 lineBreakMode:NSLineBreakByWordWrapping];
        height = size.height;
    }
    else // IOS 7 , Attributes
    {
        CGRect frame = [myHeader boundingRectWithSize:maxSize
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:attributesDictionary
                                                context:nil];
        height = frame.size.height;
    }

    return height+5;
}
Run Code Online (Sandbox Code Playgroud)


guy*_*zda 5

你可以

return UITableViewAutomaticDimension;
Run Code Online (Sandbox Code Playgroud)

编辑:哎呀,忽略以下,请参阅下面的评论.

要么

return UITableViewAutomaticDimension + padding;
Run Code Online (Sandbox Code Playgroud)