Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewAutomaticDimension not working for footer in iOS8

I have a footer in UITableView. The footer just only contains a UILabel and I want adjust footer height depend on UILabel so I have use

self.tableview.estimatedSectionFooterHeight = 140;
self.tableview.sectionFooterHeight = UITableViewAutomaticDimension;

It works well on iOS9 but not work in iOS8 enter image description here

Here is my footer view enter image description here The UILabel has constraints: top=5, leading/trailing=8, bottom=34, line=0, height >= 18

Here is my code for 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;
}

How can I make it work in iOS8? Any help or suggestion would be great appreciated.
If you can not understand my problem, please check the DEMO PROJECT

UPDATE In some case, my footer will more complex, it doesn't only contains one UILabel, and also UILabel,UIButton will have different size for iPhone and iPad. Therefore, it's really hard to calculate footer height correctly. I still want to use UITableViewAutomaticDimension

like image 881
Linh Avatar asked May 30 '16 02:05

Linh


3 Answers

You can use this -

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

Please try it , it's work for me or feel free.

like image 109
Abhishek Mishra Avatar answered Nov 01 '22 07:11

Abhishek Mishra


Well, this seems odd. Your code is correct and you have set your constraints perfectly.

Have you set the label of your footerView with number of lines to 0 ?? Reload the view again if still issue persists.

like image 2
Sushree Swagatika Avatar answered Nov 01 '22 05:11

Sushree Swagatika


So I got this to work by calculating the height based on the text programatically:

#import "ViewController.h"
#import "BaseFooterViewCell.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableview.delegate = self;

    self.tableview.dataSource = self;

//    Comment these out so that the table will call #heightForFooterInSection
//    self.tableview.estimatedSectionFooterHeight = 140;
//    
//    self.tableview.sectionFooterHeight = UITableViewAutomaticDimension;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

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

    //Gett the cell text content
    NSString* textForCell = @"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.";


    NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"BaseFooterViewCell" owner:self options:nil];

    BaseFooterViewCell *myFooterView = [nibViews objectAtIndex: 0];

    myFooterView.contentLabel.text = textForCell;

    // Set the height constraint as an outlet in the cell's class so that you can adjust it to set the
    // right size. Also, The -16 in the andMaxWidth param is to account for the leading/trailing width on the label
    myFooterView.cellLabelHeightConstraint.constant = [self heightForString:textForCell withFont:[UIFont systemFontOfSize:15.0] andMaxWidth:self.view.frame.size.width - 16];

    [myFooterView layoutIfNeeded];

    [tableView layoutIfNeeded];

    return myFooterView;
}

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

    //this would be just getting the content for the label and calling the heightForString Method
    NSString* textForCell = @"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.";

    // the +39 at the end of the return is to account for the 34point bottom space and the 5point
    // top space on the label
    return [self heightForString:textForCell withFont:[UIFont systemFontOfSize:15.0] andMaxWidth:self.view.frame.size.width - 16] + 39;

}


-(CGFloat) heightForString:(NSString*)string withFont:(UIFont*)font andMaxWidth:(CGFloat)width {

    CGRect rect  = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                     attributes:@{NSFontAttributeName: font}
                                        context:nil];

    CGFloat height = ceilf(rect.size.height);

    return height;

}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = @"aA";
    return cell;
}

@end

And your BaseFooterViewCell.h with the added outlet

#import <UIKit/UIKit.h>

@interface BaseFooterViewCell : UIView


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cellLabelHeightConstraint;

@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@end

and the changed code and be downloaded from here: Dropbox download of changed code

And just a side note: if in your actual project your not using the system font and you are using a custom font or alternate font etc... you will need to make that change in the calls to the heightForString Method

Result of my changes on ios 8.1 simulator

like image 1
mduttondev Avatar answered Nov 01 '22 06:11

mduttondev