forkingdog / UITableView-FDTemplateLayoutCell

Template auto layout cell for automatically UITableViewCell height calculating
MIT License
9.94k stars 2.01k forks source link

Height maybe larger of iOS10.3. #311

Closed ZYongjie closed 7 years ago

ZYongjie commented 7 years ago

I have a problem that the height of cell maybe larger.I am using iPhone 6 Plus - iOS 10.3. I think this maybe system's bug. Could you help me? Heartfelt thanks!

This is my code :

#import "ViewController.h"
#import "Classes/UITableView+FDTemplateLayoutCell.h"
#import "Masonry/Masonry.h"

@interface TestCell : UITableViewCell

@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *descriptionLabel;

- (void)setData;

@end

@implementation TestCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

        self.avatarImageView = [UIImageView new];
        self.avatarImageView.layer.cornerRadius = 4 / 2.0;
        self.avatarImageView.clipsToBounds = YES;
        self.nameLabel = [UILabel new];
        self.nameLabel.font = [UIFont systemFontOfSize:17];
        self.descriptionLabel = [UILabel new];
        self.descriptionLabel.numberOfLines = 0;
        self.descriptionLabel.font = [UIFont systemFontOfSize:12];
        [self.descriptionLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self.descriptionLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        [self.contentView addSubview:self.avatarImageView];
        [self.contentView addSubview:self.nameLabel];
        [self.contentView addSubview:self.descriptionLabel];
        [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).mas_offset(20);
            make.left.equalTo(self.contentView).mas_offset(15);
            make.width.height.mas_equalTo(50);
        }];
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.avatarImageView);
            make.left.equalTo(self.avatarImageView.mas_right).mas_offset(10);
            make.right.equalTo(self.contentView).mas_offset(-10);
        }];
        [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.avatarImageView.mas_bottom).mas_offset(10);
            make.left.equalTo(self.avatarImageView);
            make.right.equalTo(self.contentView).mas_offset(-15);
            make.bottom.equalTo(self.contentView).mas_offset(-10);
        }];
    }
    return self;
}

+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

- (void)updateConstraints {
    [self.avatarImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(20);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.width.height.mas_equalTo(50);
    }];
    [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.avatarImageView);
        make.left.equalTo(self.avatarImageView.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-10);
    }];
    [self.descriptionLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarImageView.mas_bottom).mas_offset(10);
        make.left.equalTo(self.avatarImageView);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];

    [super updateConstraints];
}

- (void)setData {
    self.nameLabel.text = @"model.teacherName";
//    self.descriptionLabel.text = @"假设被大家看尽快哈不会能尽快发货看哈地方见客户即可讲课";
    self.descriptionLabel.text = @"假设被大家看尽快哈不会能尽快发货看哈地方见";
    [self setNeedsUpdateConstraints];
}

@end

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[TestCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(id cell) {
        [((TestCell *)cell) setData];
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setData];
    return cell;
}

@end

image 70210579-da7c-4020-8b49-a2b1d12d74ae

If I don't set self.descriptionLabel.numberOfLines = 0, the height is always right.

ZYongjie commented 7 years ago

I forget to set self.descriptionLabel.preferredMaxLayoutWidth.