【发布时间】:2015-11-18 03:59:56
【问题描述】:
到目前为止,我一直在计算自定义单元格高度。我需要在获取数据后自动调整单元格的大小。
self.countLabel 和self.descriptionLabel 只占用一行文本,另一方面,self.detailLabel 可能包含多行。所以下面的代码是我设置约束的方式。
CustomCell.m
- (void)initView
{
self.countLabel = [UILabel new];
self.countLabel.text = @"AAAAAA";
self.countLabel.font = [UIFont fontWithPixel:22];
self.countLabel.numberOfLines = 1;
[self.contentView addSubview:self.countLabel];
self.detailLabel = [UILabel new];
self.detailLabel.text = @"Number of people: ";
self.detailLabel.font = [UIFont fontWithPixel:22];
self.detailLabel.textColor = [UIColor qu_grayColor];
[self.contentView addSubview:self.detailLabel];
self.descriptionLabel = [UILabel new];
self.descriptionLabel.numberOfLines = 1;
self.descriptionLabel.text = @"ABCDEFGHIJKLM";
self.descriptionLabel.font = [UIFont fontWithPixel:26];
[self.contentView addSubview:self.descriptionLabel];
[self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(20);
make.top.equalTo(self.contentView).offset(5);
make.right.equalTo(self.contentView).offset(-10);
}];
[self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(20);
make.top.equalTo(self.countLabel.mas_bottom);
make.bottom.equalTo(self.descriptionLabel.mas_top);
make.right.equalTo(self.contentView).offset(-10);
}];
[self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView).offset(10);
make.right.equalTo(self.contentView).offset(-10);
make.bottom.equalTo(self.contentView).offset(-15);
}];
}
【问题讨论】:
-
如果您的目标是 iOS 9+,这可以使用 stackviews 轻松完成。您需要在垂直堆栈视图中添加标签。使用自动布局将 stackview 定位在单元格中。
-
@TheAppMentor 哎呀,这个应用程序需要支持到iOS7.0。
标签: ios objective-c uitableview autolayout masonry