我正在尝试使用自动布局自动调整表格单元格的大小。但是 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/