Ios 多行UILabel有时完全显示,有时被切断

Ios 多行UILabel有时完全显示,有时被切断,ios,objective-c,ios8,uikit,ios-autolayout,Ios,Objective C,Ios8,Uikit,Ios Autolayout,自从iOS 8发布以来,我在查看显示视图中遇到了一个奇怪的问题,我的长多行标题并不总是显示在屏幕上的完整标题。使此问题更令人沮丧的是,显示不一致,有时显示完整的标题,有时则将其切断: 在我的ViewController中,cellForRowAtIndexPath方法如下所示: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

自从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;
}
 // 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];
我的视图代码中init的相关部分如下所示:

- (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;
}
 // 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);
}];

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

尝试使用自调整单元格api。只要您的单元格设置了正确的约束条件,您所需要做的就是在viewDidLoad中添加几行


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

是的,正如rdelmar提到的,自调整单元格大小可以帮助您实现这一点。需要注意的一个关键问题是,uilabel应正确设置自动布局约束,并且行数应设置为0。

是否有原因不使用新的自调整单元格api?@rdelmar我不熟悉它。对于iOS来说,一般来说都是比较新的。你有链接吗?自调整单元格API是本机objective-c库,还是我需要安装可可豆荚?@Matt,它是本机的。有一个WWDC的视频,我想是2014年的。问你们一个问题。首先是什么导致了这种行为?文章中的图像来自运行相同模拟的完全相同的代码。我点击评论,显示它,点击后退,然后点击评论再次显示它。相同的操作,不同的结果。@Matt,当您在视图之间来回移动时,会发生一些布局操作,因此在您这样做时,必须更新某些内容。是否有关于调试此操作的建议?负荷视图?视图加载?你会出现吗?