Как использовать автопрокладку Masonry UItableviewCell height

Я пытаюсь автоматически изменить размер ячейки таблицы с помощью автоматического макета. Но кажется, что TableView игнорирует ограничения по высоте.

UIImage и UILabel в UITableViewCell:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.icon = [[UIImageView alloc] init];
        [self.contentView addSubview:self.icon];
        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(self.contentView).with.offset(10);
        make.left.equalTo(self.contentView).with.offset(10);
        make.width.offset(70);
        make.height.offset(70);
//            make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
        }];
        self.icon.backgroundColor = [UIColor redColor];

        self.customText = [[UILabel alloc] init];
        self.customText.text = @"xx11dragon";
        self.customText.backgroundColor = [UIColor yellowColor];
        self.customText.numberOfLines = 0;
        self.customText.lineBreakMode = NSLineBreakByCharWrapping;
        [self.contentView addSubview:self.customText];

        [self.customText mas_makeConstraints:^(MASConstraintMaker *make) {

            make.top.equalTo(self.contentView).with.offset(10);
            make.left.equalTo(self.icon.mas_right).with.offset(10);
            make.right.equalTo(self.contentView.mas_right).with.offset(-10);
//            make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);

        }];

В UITableViewClass:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
    _cell.title.text = [dict objectForKey:@"title"];
    _cell.customText.text = [dict objectForKey:@"content"];
    [_cell setNeedsLayout];
    [_cell layoutIfNeeded];
    CGSize size = [_cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    NSLog(@"%f",size.height);


    return size.height + 1;
}

size.height равно 0.0f

Высота UITableViewCell равна UIimage или UIlabel bottom + 10px, как настроить дно?


person xx11dragon    schedule 19.08.2015    source источник


Ответы (1)


В iOS 8 вы можете использовать этот подход.

self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
person Tigran Torgomyan    schedule 19.08.2015