Closed JsonFu closed 7 years ago
同样遇到此问题, 使用的是 1.5-beta版本 测试使用SnapKit 设置Cell 调用时 // Auto layout engine does its math fittingHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; systemLayoutSizeFittingSize 该方法一直获取不到 AutoLayout的高度? 目前正尝试降低版本测试
@JsonFu 出现此问题是你的Cell 在设置Autolayout时,没有设置子控件距离父类底部的距离导致无法计算出高度。所以为0
会不会是 label.preferredMaxLayoutWidth 的问题?
@afantree 不是
我也出现这个问题,用的是系统的textLabel
(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath { LogTableViewCell cell = [LogTableViewCell cellWithTableView:self Identifier:@"cell"]; [[cell textLabel] setText:@"ahfhgvgy合适的,难看了健康了健康来不及会不会就不会就不会就不喝酒不就不会就不会就不会就不会被vhewiufh"];
return cell; }
(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath { return [self fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(id cell) { [[cell textLabel] setText:@"ahfhgvgy合适的,难看了健康了健康来不及会不会就不会就不会就不喝酒不就不会就不会就不会就不会被vhewiufh"]; }]; }
我也遇到了这个问题,查证后发现是因为子控件没有从上到下没有形成闭合的约束(或者最下面子控件距离父控件的位置约束等级设置为priorityLow)
还有一个可能的原因是tableView的宽度在某种特殊情况下为0
(CGFloat)tableView:(UITableView)tableView heightForRowAtIndexPath:(NSIndexPath)indexPath {
WPFMessage *message = self.messages[indexPath.row]; CGFloat height = message.height;
if (!height) { __weak WPFMessageViewController *weakSelf = self; CGFloat tableViewWidth = tableView.frame.size.width;
if (tableViewWidth > 0){
height = [self.tableView
fd_heightForCellWithIdentifier:identifier
cacheByIndexPath:indexPath
configuration:^(ChannelMessageBaseCell* cell) {
[weakSelf configureCell:cell atIndexPath:indexPath];
}];
}
message.height = height;
} return height; }
确定自己的约束是写的没有问题的. Cell的代码如下:
#import "PHEQAQuesInfoTableViewCell.h"
//跳转
//Model
//系统框架
//常量
#import "MCUIRelated.h"
// #import "MCInternationalString.h"
// #import "MSCommonString.h"
//第三方
//工具类或类别
#import "UIButton+EVOQuickInit.h"
NSString *const kPHEQAQuesInfoTableViewCell = @"PHEQAQuesInfoTableViewCell" ;
static NSString *const kUnfold = @"展开";
@interface PHEQAQuesInfoTableViewCell ()
@end
@implementation PHEQAQuesInfoTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.backgroundColor = UICOLOR_EVO_PURE_WHITE;
[self addSubviews];
}
return self;
}
- (void)addSubviews{
[self.contentView addSubview:self.userHeaderImageView];
[self.contentView addSubview:self.userNameLabel];
[self.contentView addSubview:self.QACurrentState];
[self.contentView addSubview:self.questionDescribeTextView];
[self.contentView addSubview:self.unfoldQuestion];
[self.contentView addSubview:self.QAPriceLabel];
}
- (void)bindWithViewModel:(id)viewModel{
}
- (void)addConstraints{
[self.userHeaderImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.contentView).offset(EVO_PIX_15);
make.left.mas_equalTo(self.contentView).offset(EVO_PIX_15);
make.width.mas_equalTo(EVO_PIX_25);
make.height.mas_equalTo(EVO_PIX_25);
make.bottom.mas_equalTo(self.questionDescribeTextView);
}];
[self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.userHeaderImageView);
make.left.mas_equalTo(self.userHeaderImageView.mas_right).offset(EVO_PIX_10);
}];
[self.QACurrentState mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.userNameLabel);
make.right.mas_equalTo(self.contentView).offset(-EVO_PIX_15);
}];
[self.questionDescribeTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.userHeaderImageView);
make.top.mas_equalTo(self.userHeaderImageView.mas_bottom).offset(EVO_PIX_15);
make.bottom.mas_equalTo(self.contentView);
make.right.equalTo(self.contentView).offset(-EVO_PIX_15);
}];
// FIXME: 在这里这个Button的东西我有点不知道该怎么写, 应该是判断出来状态之后再去写他的布局
[self.QAPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.userHeaderImageView);
make.top.mas_equalTo(self.questionDescribeTextView.mas_bottom).offset(EVO_PIX_15);
make.bottom.mas_equalTo(self.contentView).offset(-EVO_PIX_15);
}];
}
- (void)layoutSubviews{
[super layoutSubviews];
[self addConstraints];
}
#pragma mark -Getter And Setter
-(void)setFrame:(CGRect)frame{
frame.origin.x = EVO_PIX_5;//这里间距为10,可以根据自己的情况调整
frame.size.width -= 2 * frame.origin.x;
frame.size.height -= 2 * frame.origin.x;
[super setFrame:frame];
}
- (UIImageView *)userHeaderImageView{
if (!_userHeaderImageView) {
self.userHeaderImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_userHeaderImageView.layer.masksToBounds = YES;
_userHeaderImageView.layer.cornerRadius = EVO_PIX_25/2;
}
return _userHeaderImageView;
}
- (UILabel *)userNameLabel{
if (!_userNameLabel) {
self.userNameLabel = [[UILabel alloc] init];
_userNameLabel.font = UIFONT_IOS_DEFAULT_14;
_userNameLabel.textColor = UICOLOR_SOHU_PHE_G1;
}
return _userNameLabel;
}
- (UILabel *)QACurrentState{
if (!_QACurrentState) {
self.QACurrentState = [[UILabel alloc] init];
_QACurrentState.font = UIFONT_IOS_DEFAULT_12;
_QACurrentState.textColor = UICOLOR_SOHU_PHE_G3;
}
return _QACurrentState;
}
- (YYTextView *)questionDescribeTextView{
if (!_questionDescribeTextView) {
self.questionDescribeTextView = [[YYTextView alloc] init];
_questionDescribeTextView.font = UIFONT_IOS_DEFAULT_15;
_questionDescribeTextView.textColor = UICOLOR_SOHU_PHE_G1;
}
return _questionDescribeTextView;
}
- (UILabel *)QAPriceLabel{
if (!_QAPriceLabel) {
self.QAPriceLabel = [[UILabel alloc] init];
_QAPriceLabel.font = UIFONT_IOS_DEFAULT_13;
_QAPriceLabel.textColor = UICOLOR_SOHU_PHE_G3;
}
return _QAPriceLabel;
}
- (UIButton *)unfoldQuestion{
if (!_unfoldQuestion) {
self.unfoldQuestion = [UIButton initButtonWithText:kUnfold];
[_unfoldQuestion setTitleColor:UICOLOR_SOHU_PHE_B1 forState:UIControlStateNormal];
_unfoldQuestion.titleLabel.font = UIFONT_IOS_DEFAULT_15;
}
return _unfoldQuestion;
}
- (UILabel *)test{
if (!_test) {
self.test = [[UILabel alloc] init];
_test.numberOfLines = 0;
_test.lineBreakMode = NSLineBreakByWordWrapping;
}
return _test;
}
@end
TableView所使用的两个关键的代理方法如下:
/*************** UITableView DataSource *****************/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
PHEQAQuesInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kPHEQAQuesInfoTableViewCell forIndexPath:indexPath];
PHEQAQuestionModel *model = self.viewModel.questionModelArray[indexPath.row];
if (nil!= model.fromUser.portraitUrl &&![model.fromUser.portraitUrl isEqualToString:@""] ) {
cell.userHeaderImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.fromUser.portraitUrl]]];
}else{
cell.userHeaderImageView.image = [UIImage imageNamed:@"tab_mine"];
}
cell.userNameLabel.text = model.toUser.name;
cell.QACurrentState.attributedText = model.questionCurrentState;
cell.questionDescribeTextView.text = model.content;
cell.QAPriceLabel.text = model.priceDescribe;
return cell ;
}
/************* UITableView Delegate *******************/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [tableView fd_heightForCellWithIdentifier:kPHEQAQuesInfoTableViewCell cacheByIndexPath:indexPath configuration:^(PHEQAQuesInfoTableViewCell *cell) {
PHEQAQuestionModel *model = self.viewModel.questionModelArray[indexPath.row];
if (nil!= model.fromUser.portraitUrl &&![model.fromUser.portraitUrl isEqualToString:@""] ) {
cell.userHeaderImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.fromUser.portraitUrl]]];
}else{
cell.userHeaderImageView.image = [UIImage imageNamed:@"tab_mine"];
}
cell.userNameLabel.text = model.toUser.name;
cell.QACurrentState.attributedText = model.questionCurrentState;
cell.questionDescribeTextView.text = model.content;
cell.QAPriceLabel.text = model.priceDescribe;
}];
}
而我会出现如下的日志:
请阳神帮忙看一下, 不觉得自己哪里写的有问题啊...
[FDTemplateLayoutCell] Warning once only: Cannot get a proper cell height (now 0) from '- systemFittingSize:'(AutoLayout). You should check how constraints are built in cell, making it into 'self-sizing' cell.是不是因为约束不够的问题。