ios - Masonry如何使用autolayout UItableviewCell高度

标签 ios objective-c iphone autolayout masonry

我正在尝试使用自动布局自动调整表格单元格的大小。但是 TableView 似乎忽略了高度限制。

UITableViewCell 中的 UIImage 和 UILabel:

- (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,如何设置bottom?

最佳答案

在 iOS 8 中,您可以使用这种方法。

self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;

关于ios - Masonry如何使用autolayout UItableviewCell高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32088825/

相关文章:

ios - 条件绑定(bind)中的绑定(bind)值必须是可选类型 [IOS - SWIFT]

iphone - Objective-C : How to fetch the router address?

iphone - 有没有办法为 UITableViewCell 使用随机项目 png 图像?

ios - 如何在 swift 4 中重新排序单元格后保存 tableView 顺序

ios - Xcode 警告 : . .. 是 GNU 扩展

iphone - 维基百科作为我的 iOS 应用程序的一部分

objective-c - 不同版本的 Mac OS X 之间的代码分开吗?

iphone - 您可以在 iPhone 应用程序中做哪些最耗电的事情?

iphone - iPhone中的音频处理

iphone - UIScrollView 和 setContentOffset