0

自 iOS 8 发布以来,我在评论显示视图中遇到了一个奇怪的问题,我的长多行标题并不总是在屏幕上显示完整标题。让这个问题更令人沮丧的是显示不一致,有时会显示完整的标题,有时会像这样将其截断:

截取标题截图 完全显示的标题截图

在我的 ViewController 中,该cellForRowAtIndexPath方法如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Since we have multi-line labels, do an initial layout pass and then set the preferredMaxLayoutWidth
    // based on their width so they will wrap text and take on the correct height
    [self.cell.contentView setNeedsLayout];
    [self.cell.contentView layoutIfNeeded];

    [self.cell.titleLabel setPreferredMaxLayoutWidth:CGRectGetWidth(self.cell.contentView.frame)];
    [self.cell.titleLabel sizeToFit];

    [self.cell.descriptionLabel setPreferredMaxLayoutWidth:CGRectGetWidth(self.cell.contentView.frame)];
    [self.cell.descriptionLabel sizeToFit];

    return self.cell;
}

我的视图代码中的相关部分init如下所示:

 // title label
    self.titleLabel = [[UILabel alloc] init];
    [self.titleLabel setFont:[UIFont fontWithName:[StringFactory defaultFontType] size:kTitleFontSize]];
//    if (self.review.title)
//    {
//        [self.titleLabel setText:[NSString stringWithFormat:@"\"%@\"", self.review.title]];
//    }
//    else
//    {
//        [self.titleLabel setText:@""];
//    }
    [self.titleLabel setText:@"\"This title should be too long to fit on one line. If it isn't, we have bigger problems.\""];
    [self.titleLabel setTextColor:[ColorFactory CC333]];
    [self.titleLabel setAdjustsFontSizeToFitWidth:NO];
    [self.titleLabel setNumberOfLines:0];
    [self.titleLabel sizeToFit];
    [self.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.titleLabel setBackgroundColor: [ColorFactory CCOrange]];
    [self.contentView addSubview:self.titleLabel];

这是约束设置(使用砌体):

typeof (self) __weak weakSelf = self;

// title
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.contentView.mas_top).with.offset(kPadding);
    make.left.equalTo(weakSelf.contentView.mas_left).with.offset(kPadding);
    make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-kPadding);
}];

[self.ratingBubbles mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.titleLabel.mas_bottom).with.offset(5);
    make.left.equalTo(weakSelf.contentView.mas_left).with.offset(kPadding);
    make.height.equalTo(weakSelf.bubbleHeight);
    make.width.equalTo(weakSelf.bubbleWidth);
}];

[self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.titleLabel.mas_bottom).with.offset(5);
    make.left.equalTo(weakSelf.ratingBubbles.mas_right).with.offset(kPadding);
    make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-kPadding);
}];


// description
[self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(weakSelf.ratingBubbles.mas_bottom).with.offset(5);
    make.left.equalTo(weakSelf.contentView.mas_left).with.offset(kPadding);
    make.right.equalTo(weakSelf.contentView.mas_right).with.offset(-kPadding);
    make.bottom.equalTo(weakSelf.contentView.mas_bottom).with.offset(-kPadding);
}];

在过去两个月左右的时间里,我一直在尝试解决这个问题,断断续续,但到目前为止还没有任何运气。如果有人遇到过类似的问题,请发表您的经验。

4

2 回答 2

0

尝试使用自定尺寸单元 api。只要您的单元格正确设置了约束,您需要做的就是在viewDidLoad,

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 50;
}


-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated]; 
    [self.tableView reloadData]; // this may or may not be necessary I found that cells didn't size correctly until scrolled or rotated without this line in iOS 8.3
}

您的约束需要从单元格顶部到任何视图到底部是连续的。您不需要您当前拥有的任何代码cellForRowAtIndexPath;除了使单元格出列之外,您应该在那里做的唯一一件事就是设置标签的文本。在单元格类中,您不需要sizeToFit,也不应该在那里设置文本。

于 2015-05-13T19:34:55.777 回答
0

是的,如 rdelmar 所述,自定尺寸单元可以帮助您解决此问题。需要注意的一件事是 uilabel 应该正确设置自动布局约束,并且行数应该设置为 0。

于 2015-05-13T20:35:20.423 回答