自定义UITableViewCell自动布局不起作用

时间:2019-07-23 00:39:02

标签: ios objective-c uitableview autolayout

我正尝试通过自动布局来实现自定义表格单元,如下所示,但是由于某种原因,我没有得到预期的结果。

预期

enter image description here

实际

enter image description here

我的观察是:

  • 单元格高度不会随着内容的增长而增加,并且内容会溢出;
  • bar元素应该是垂直的蓝色条,但显示不正确;
  • 出于某种原因,在UIView元素上设置背景颜色根本不起作用。

请分享一些我做错了的事情。预先感谢

UITableViewCell代码在下面

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.infoContainer = [[UIView alloc] init];
        self.title = [[UILabel alloc] init];
        self.time = [[UILabel alloc] init];
        self.bar = [[UIView alloc] init];

        [self.infoContainer addSubview:self.bar];
        [self.infoContainer addSubview:self.title];
        [self.infoContainer addSubview:self.time];
        [self.contentView addSubview:self.infoContainer];

        self.infoContainer.translatesAutoresizingMaskIntoConstraints = NO;
        self.title.translatesAutoresizingMaskIntoConstraints = NO;
        self.time.translatesAutoresizingMaskIntoConstraints = NO;
        self.bar.translatesAutoresizingMaskIntoConstraints = NO;

        [self.infoContainer.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:18].active = YES;
        [self.infoContainer.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant:-18].active = YES;
        [self.infoContainer.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:10].active = YES;
        self.infoContainer.backgroundColor = [UIColor yellowColor];

        [self.bar.leftAnchor constraintEqualToAnchor:self.infoContainer.leftAnchor constant:0].active = YES;
        [self.bar.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;
        [self.bar.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:0].active = YES;
        [self.bar.heightAnchor constraintEqualToAnchor:self.infoContainer.heightAnchor].active = YES;
        [self.bar.widthAnchor constraintEqualToConstant:10];
        self.bar.backgroundColor = [UIColor blueColor];

        [self.title.leftAnchor constraintEqualToAnchor:self.bar.rightAnchor constant:15].active = YES;
        [self.title.rightAnchor constraintEqualToAnchor:self.infoContainer.rightAnchor constant:0].active = YES;
        [self.title.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;

        [self.time.leftAnchor constraintEqualToAnchor:self.title.leftAnchor constant:0].active = YES;
        [self.time.rightAnchor constraintEqualToAnchor:self.title.rightAnchor constant:0].active = YES;
        [self.time.topAnchor constraintEqualToAnchor:self.title.bottomAnchor constant:10].active = YES;
    }

    return self;
}

在表格视图中,我有:

self.recentView.rowHeight = UITableViewAutomaticDimension;
self.recentView.estimatedRowHeight = 64.0f;

谢谢!

1 个答案:

答案 0 :(得分:-1)

自动高度完全取决于上下挂钩的约束,因此您会错过2个约束

1-

[self.infoContainer.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:10].active = YES;

2-

[self.time.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:10].active = YES;

提示::如果您在此处设置顶部和底部约束

[self.bar.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;
[self.bar.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:0].active = YES;

然后不需要

[self.bar.heightAnchor constraintEqualToAnchor:self.infoContainer.heightAnchor].active = YES;