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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21085627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:05:57  来源:igfitidea点击:

Header/Footer height of a UITableView section based on title text height

iosobjective-cxcodeuitableview

提问by DBoyer

I am wondering if its possible to make the height header/footer for a section of a UITableView be equal to the height of the header/footer title text. Any tips would be great!

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

Note: some sections of my TableView may not have a header/footer and in that case would just have the padding between sections since the "headerTitleHeight/footerTitleHeight" would be zero in this case.

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

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

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

回答by Xeieshan

-(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;
}

回答by guywithmazda

You can just

你可以

return UITableViewAutomaticDimension;

EDIT: Oops, ignore the following, see comments below.

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

or

或者

return UITableViewAutomaticDimension + padding;

回答by Markicevic

You can you automatic dimensions but you have to have estimated size. It works for Footer and Header the same way.

你可以自动尺寸,但你必须有估计的尺寸。它以相同的方式适用于页脚和页眉。

func tableView(_ tableView: UITableView, estimatedHeightForFooterInSection section: Int) -> CGFloat {
    return 300
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return UITableViewAutomaticDimension
}

回答by NANNAV

Calculate size of text and add padding height

计算文本大小并添加填充高度

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{
CGFloat headerTitleHeight = [jobTitleString sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:20.0f] constrainedToSize:CGSizeMake(width,9999)].height;
    return headerTitleHeight + padding;
}

回答by Phan Van Linh

In viewDidLoad, heightForFooterInSectionand heightForHeaderInSection

viewDidLoad,heightForFooterInSectionheightForHeaderInSection

- (void)viewDidLoad {
   [super viewDidLoad];

   self.tableView.estimatedSectionFooterHeight = 50; // for footer
   self.tableView.estimatedSectionHeaderHeight = 50; // for header
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return UITableViewAutomaticDimension;
}

In Footer/Header xib file
- Add BOTH constraint top and bottomto ContainerViewfor your UILabel
- Add UILabel constraint height then change UILabel constraint height from Equal-> Greater than or Equals
- Finaly change UILabel line to 0

在页脚/头XIB文件
-添加BOTH约束顶部和底部,以ContainerView你的UILabel
-添加的UILabel约束高度然后从改变的UILabel约束高度Equal- > Greater than or Equals
- Finaly变化的UILabel线0

enter image description here

在此处输入图片说明

回答by Praveen S

You could use boundingRectWithSizemethod to get the height required to draw the particular NSString for the header/footer and then return the values in the UITableView delegate methods. For sections without header footer, you have to code to just return the padding.

您可以使用boundingRectWithSize方法获取为页眉/页脚绘制特定 NSString 所需的高度,然后返回 UITableView 委托方法中的值。对于没有页眉页脚的部分,您必须编码以仅返回填充。

回答by RAJA

You can try the below..

你可以试试下面的..

CGSize constraint = CGSizeMake(<header/footer width>, <max header/footer height>);

CGSize size;

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
    NSRange range = NSMakeRange(0, [<string> length]);

    NSDictionary *attributes = [<string> attributesAtIndex:0 effectiveRange:&range];
    CGSize boundingBox = [<string> boundingRectWithSize:constraint options: NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
}
else
{
    size = [<string> sizeWithFont:descriptionLabel.font constrainedToSize:constraint lineBreakMode:descriptionLabel.lineBreakMode];
}

return size.height + padding;

or

或者

CGSize maximumLabelSize = CGSizeMake(<header/footer width>, <max header/footer height>);

CGSize expectedSize = [<string label> sizeThatFits:maximumLabelSize];

return expectedSize.height + padding;