簡體   English   中英

UITableViewAutomaticDimension在iOS8中不適用於頁腳

[英]UITableViewAutomaticDimension not working for footer in iOS8

我在UITableView有一個頁腳。 頁腳只包含一個UILabel ,我想調整頁腳高度取決於UILabel所以我有用

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

它在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;
}

如何在iOS8中使其工作? 任何幫助或建議將非常感謝。
如果您無法理解我的問題,請查看DEMO PROJECT

更新在某些情況下,我的頁腳將更復雜,它不僅包含一個UILabel ,而且UILabelUIButton將具有不同的iPhoneiPad大小。 因此,正確計算頁腳高度真的很難。 我仍然想使用UITableViewAutomaticDimension

你可以用這個 -

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

請試一試,這對我有用或感覺自由。

嗯,這看起來很奇怪。 您的代碼是正確的,您已完全設置了約束。

你有沒有將footerView的標簽設置為0? 如果問題仍然存在,請重新加載視圖。

所以我通過以編程方式基於文本計算高度來實現這一點:

#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

和你的BaseFooterViewCell.h添加插座

#import <UIKit/UIKit.h>

@interface BaseFooterViewCell : UIView


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

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

@end

和更改的代碼,從這里下載Dropbox下載更改的代碼

還有一個注意事項:如果在你的實際項目中你沒有使用系統字體而你正在使用自定義字體或替代字體等......你需要在調用heightForString方法時進行更改

我在ios 8.1模擬器上的更改結果

您的頁腳布局約束是正確的,我認為您不能在iOS 8中使用UITableViewAutomaticDimension作為頁腳。

對於iOS8,您應該以編程方式計算頁腳高度,不要設置estimatedSectionFooterHeigh或設置estimatedSectionFooterHeight = 0

對於iOS9及以上版本,您可以正常使用UITableViewAutomaticDimension請查看此參考: https//github.com/ebetabox/DynamicCellSectionHeight

經過測試,這是我的結果

對於iOS8, UITableViewAutomaticDimension 僅適用於節標題和tableview單元格, 不適用於節頁腳

奇怪的是,如果我的tableview 使用節頭和頁腳, 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)];
}

對於iOS9及更高版本, UITableViewAutomaticDimension正常工作

在此輸入圖像描述

只需對標簽賦予四個約束: Top,leading,trailing and bottom 不要給予身高限制。

並在viewWillAppearviewDidAppear重新加載inputViews的inputViews,

   [self.tableView.tableFooterView reloadInputViews];

更新:

嘗試實現heightForFooterInSection如,

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

     return UITableViewAutomaticDimension;
}

您需要在IB或代碼中設置並刪除高度約束

[yourLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[yourLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

並在設置文本后調用此方法

[self setNeedsDisplay];
[self layoutIfNeeded];

BaseFooterViewCell類中

希望這有幫助。 我從Apple的Self Sizing示例中獲取了這個

首先我要說的是“你使用的魔法值140實際上非常接近這個文本矩形”。 更改此值,您會發現它也無法在iOS 9上運行。 並從標簽中刪除該高度約束,然后檢查它在兩個IOS版本上都不起作用。 這個高度約束在IOS 8中有點亂。那就是高度> =約束可能IOS有一個bug。

現在對sectionFooterHeight的一些澄清是一個estimatedSectionFooterHeight 這個部分.FooterHeight jus為IOS提供了一個靜態值,用於調整頁腳大小。 estimatedSectionFooterHeight只是改善了性能,例如估計的高度應該接近實際高度所以IOS不必重繪大視圖。 (我對官方文件的理解

剩下的就是你必須在運行時計算標簽的大小。然后創建適當的頁腳視圖並返回它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM