layoutIfNeeded 就这样把我害惨

it2024-12-13  14

在我的项目里面,我自定义了一个cell,然后用masonry进行约束,tableview的cell自适应label的高度

cell里面:

#import "MedicalRecordCell.h" @interface MedicalRecordCell () @property (weak, nonatomic) UILabel *fieldnameLabel; @property (weak, nonatomic) UILabel *uomLabel; @property (weak, nonatomic) UILabel *valueLabel; @property (weak, nonatomic) UIView *lineView; @end static CGFloat const margin = 10; @implementation MedicalRecordCell - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) { UILabel *fieldnameLabel = [UILabel label]; fieldnameLabel.textColor = [UIColor darkGrayColor]; fieldnameLabel.font = [UIFont boldSystemFontOfSize:17]; fieldnameLabel.textAlignment = NSTextAlignmentLeft; fieldnameLabel.backgroundColor = RANDOMCOLOR(0.6); [self.contentView addSubview:fieldnameLabel]; self.fieldnameLabel = fieldnameLabel; UILabel *uonLabel = [UILabel label]; [self.contentView addSubview:uonLabel]; self.uomLabel = uonLabel; UILabel *valueLabel = [UILabel label]; valueLabel.backgroundColor = RANDOMCOLOR(0.6); valueLabel.textAlignment = NSTextAlignmentRight; valueLabel.numberOfLines = 0; valueLabel.font = [UIFont systemFontOfSize:15]; [self.contentView addSubview:valueLabel]; self.valueLabel = valueLabel; UIView *lineView = [UIView new]; lineView.backgroundColor = CELLLINE_COLOR; [self.contentView addSubview:lineView]; self.lineView = lineView; [self setViewAutoLayout]; } return self; } - (void)setViewAutoLayout { [self.fieldnameLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.contentView).offset(margin); make.top.mas_equalTo(self.contentView).offset(margin); }]; [self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.fieldnameLabel.mas_right).offset(margin); make.right.mas_equalTo(self.uomLabel.mas_left).offset(-margin); make.top.mas_equalTo(self.contentView).offset(margin); make.bottom.mas_equalTo(self.contentView).offset(-margin); }]; [self.uomLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.contentView).offset(-margin); make.width.offset(40); make.height.offset(40); make.centerY.equalTo(self.contentView); }]; [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.contentView); make.height.offset(1); }]; } - (void)setMedicalrecordSectionModel:(MedicalrecordSectionModel *)medicalrecordSectionModel { _medicalrecordSectionModel = medicalrecordSectionModel; self.fieldnameLabel.text = [NSString stringWithFormat:@"%@ :",medicalrecordSectionModel.fieldname]; self.uomLabel.text = medicalrecordSectionModel.uom; self.valueLabel.text = medicalrecordSectionModel.value; if ([medicalrecordSectionModel.metatype isEqualToString:@"radio"]) { NSArray *options = [medicalrecordSectionModel.metatextassemble componentsSeparatedByString:@"|"]; NSInteger value = [medicalrecordSectionModel.value integerValue]; if(value) self.valueLabel.text = options[value - 1]; } } @end

控制器设置tableview自适应

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

 

然而,不管是上面那样把约束写在初始化话方法里,还是我原先的把约束写在layoutSubviews里,都不能让内容正常的显示

 

你看看,你看看,真是可怜,tableview来回滚动的时候还偶尔会显示正常,反正就是看他心情来显示...

后来我解决的办法就是cell里面用模型赋值之后调用了一下layoutIfNeeded  没想到竟然就解决了这个问题

别人是这么说滴:

-layoutIfNeeded方法:如果,有需要刷新的标记,立即调用layoutSubviews进行布局(如果没有标记,不会调用layoutSubviews)如果要立即刷新,要先调用[view setNeedsLayout],把标记设为需要布局,然后马上调用[view layoutIfNeeded],实现布局在视图第一次显示之前,标记总是“需要刷新”的,可以直接调用[view layoutIfNeeded]

- (void)setMedicalrecordSectionModel:(MedicalrecordSectionModel *)medicalrecordSectionModel { _medicalrecordSectionModel = medicalrecordSectionModel; self.fieldnameLabel.text = [NSString stringWithFormat:@"%@ :",medicalrecordSectionModel.fieldname]; self.uomLabel.text = medicalrecordSectionModel.uom; self.valueLabel.text = medicalrecordSectionModel.value; if ([medicalrecordSectionModel.metatype isEqualToString:@"radio"]) { NSArray *options = [medicalrecordSectionModel.metatextassemble componentsSeparatedByString:@"|"]; NSInteger value = [medicalrecordSectionModel.value integerValue]; if(value) self.valueLabel.text = options[value - 1]; } [self layoutIfNeeded]; }

这样基本是可以正常显示了,但是cell的高度一直是以 valueLabel 底部约束. 那么,有个问题就出现了 ,如果 模型value没有值就会导致label没有高度,回事cell成一坨坨的哦,所以这里要给label一个默认值,让其有高度就可以了

转载于:https://www.cnblogs.com/yulongjiayuan/p/5704597.html

最新回复(0)