UITableViewAutomaticDimension在iOS8中不适用于页脚

Pha*_*inh 10 uitableview ios

我有一个页脚UITableView.页脚只包含一个UILabel,我想调整页脚高度取决于UILabel我有用

self.tableview.estimatedSectionFooterHeight = 140;
self.tableview.sectionFooterHeight = UITableViewAutomaticDimension;
Run Code Online (Sandbox Code Playgroud)

它在iOS9上运行良好,但在iOS8中无效 在此输入图像描述

这是我的页脚视图 在此输入图像描述UILabel有限制:top=5, leading/trailing=8, bottom=34, line=0, height >= 18

这是我的init footer的代码

- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"BaseFooterViewCell" owner:self options:nil];
    BaseFooterViewCell *myFooterView = [nibViews objectAtIndex: 0];
    myFooterView.contentLabel.text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
    return myFooterView;
}
Run Code Online (Sandbox Code Playgroud)

如何在iOS8中使其工作?任何帮助或建议将非常感谢.
如果您无法理解我的问题,请查看DEMO PROJECT

UPDATE 在某些情况下,我的页脚更加复杂,它不仅包含了一个UILabel,而且UILabel,UIButton将有不同的大小iPhoneiPad.因此,正确计算页脚高度真的很难.我还是想用UITableViewAutomaticDimension

Abh*_*hra 5

你可以用这个 -

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{
   // return 43;

 CGSize constraint = CGSizeMake(width, 20000.0f);
    CGSize size;

    CGSize boundingBox = [text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:font}
                                                  context:nil].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

    return size.height;
}
Run Code Online (Sandbox Code Playgroud)

请试一试,这对我有用或感觉自由.


Pha*_*inh 1

测试后,这是我的结果

对于 iOS8,UITableViewAutomaticDimension 仅适用于节标题和表格视图单元格,不适用于节页脚

奇怪的是,如果我的表格视图同时使用节页眉和页脚,则它们UITableViewAutomaticDimension将适用于页脚。

然后这是解决方案

- (void)viewDidLoad {
    // I set UITableViewAutomaticDimension for both footer and header
    self.tableview.estimatedSectionHeaderHeight = 0; // it should be 0 because header is empty
    self.tableview.sectionHeaderHeight = UITableViewAutomaticDimension;

    self.tableview.estimatedSectionFooterHeight = 140;
    self.tableview.sectionFooterHeight = UITableViewAutomaticDimension;
}
// I return empty view for header
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
Run Code Online (Sandbox Code Playgroud)

对于iOS9及以上版本,UITableViewAutomaticDimension可以正常工作

在此输入图像描述