UITableViewCell 中的几个多行标签,情节提要 xcode 6.3.2 中的自动布局

Posted

技术标签:

【中文标题】UITableViewCell 中的几个多行标签,情节提要 xcode 6.3.2 中的自动布局【英文标题】:Several multiline labels in UITableViewCell, auto layout in storyboard xcode 6.3.2 【发布时间】:2015-07-13 14:24:31 【问题描述】:

我在 tableviewcell 中的 UILabels 中遇到截断文本的问题。到目前为止,我已经阅读并测试了以下链接中的解决方案(还有许多类似的):

Multiple UILabels inside a self sizing UITableViewCell ios8 autolayout: BOTH multi-line(maybe 0 line) label Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

但如果我使用“标准”顶部、前导、尾随和底部自动布局约束设置单元格,我似乎无法让它在第一次加载时工作。 但是,如果我取消选中超级视图项的“相对于边距”并使用例如等于:默认

如果我旋转设备或再次上下滚动,这两种情况都适用。

标签与自定义单元格上的插座相连,并在原型单元格中垂直放置;它们每个都有三个超级视图约束,标签之间有一个“标准”垂直距离。最顶部标签上的拥抱优先级设置得更高,以消除 IB 中的警告,即使它与结果无关。

相关代码:

TableViewController.m

- (void)viewDidLoad 
    [super viewDidLoad];
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) 
        self.tableView.estimatedRowHeight = self.tableView.rowHeight;
        self.tableView.rowHeight = UITableViewAutomaticDimension; 
    


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    TableViewCellIB *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    [cell adjustSizeToMatchWidth:CGRectGetWidth(self.tableView.frame)];

    cell.titleLabel.text = @"Veeeeeeeery looooooooooooooooong Tiiiiitle";
    cell.messageLabel.text = @"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.";

    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];

    return cell; 

TableViewCellIB.m

- (void)adjustSizeToMatchWidth:(CGFloat)width 
    CGRect rect = self.frame;
    rect.size.width = width;
    self.frame = rect;

    rect = self.contentView.bounds;
    rect.size.height = 99999.0;
    rect.size.width = 99999.0;
    self.contentView.bounds = rect; 

未选中“相对于边距”和“等于:默认”的一个小问题是,即使标签中的文本没有被截断,使用这些设置的单元格看起来也不是很好,因为边距很远在我看来很大。是否有其他“默认”方法可以从 Storyboard 中使用它,还是我必须对约束中的 Equals 使用“幻数”?

此外,如果我以编程方式执行此操作,我将如何使其工作?

据我了解,标准视觉格式“H:|-[topLabel]-|”自 iOS 8 起默认为“相对于边距”。

提前致谢!

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,标签垂直放置在 UITableViewCell 中,并且在旋转或重新加载时可以正常工作。但是他们第一次没有正确显示。

您分享的链接和我找到的答案对我来说很神奇: https://***.com/a/26351692

    我只是在情节提要的标签中添加了顶部、底部、左侧和右侧约束。 行数 = 0,换行符 = 自动换行

    在 viewDidLoad 中,

    self.tbl_Name.estimatedRowHeight = 200.0f;
    self.tbl_Name.rowHeight = UITableViewAutomaticDimension;
    

    最后,

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath 
    // dequeue cell...
    
    // do autolayout staffs...or if the autolayout rule has been set in xib, do nothing
    
    [cell layoutIfNeeded];
    
    return cell;
    
    

所有这些,解决了我的问题。希望对你也有帮助。

NOTE::
And more importantly, this issue does NOT arise with Xcode 7 on iOS 9. Apple might have fixed it.

【讨论】:

谢谢,但这几乎是我已经拥有的设置,它并没有真正解决我遇到的问题;如果我选中了“相对于边距”,则标签的大小不正确,这是默认设置。更重要的是,如果我不想使用幻数,如何以编程方式执行此操作? [cell layoutIfNeeded] 是多余的【参考方案2】:
//set constraint properly. don't set fixed width/height. 
//also label's property no. lines=0 

//tempUserCustomCell is @property

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath


    CGFloat heightTest=0.0;

    if (!self.tempUserCustomCell) 
        self.tempUserCustomCell=[self.lifeLineTableView dequeueReusableCellWithIdentifier:@"homeCell"];

    
    //set values to cal. height
    self.tempUserCustomCell.userName.text = [self.lifeLineAllPostArray[indexPath.row] valueForKey:@"username"];
    self.tempUserCustomCell.messageLabel.text= [self.lifeLineAllPostArray[indexPath.row] valueForKey:@"desc"];
    self.tempUserCustomCell.viewsLabel.text=[NSString stringWithFormat:@"Views %d",[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"views"] intValue]];
    self.tempUserCustomCell.likeDisLikeComment.text=[NSString stringWithFormat:@"Likes %d Dislikes %d",[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"likes"] intValue],[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"dislikes"] intValue]];

    if ([[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] valueForKey:@"comments"] integerValue]==0) 
        [self.tempUserCustomCell.buttonCommentsOutlatet setTitle:@"Comment" forState:UIControlStateNormal];
    
    else
        [self.tempUserCustomCell.buttonCommentsOutlatet setTitle:[NSString stringWithFormat:@"Comments %d",[[[self.lifeLineAllPostArray objectAtIndex:indexPath.row] objectForKey:@"comments"] intValue]] forState:UIControlStateNormal];
    

    self.tempUserCustomCell.messageLabel.font=[UIFont fontWithName:@"Helvetica" size:18];

    //layout cell
    [self.tempUserCustomCell layoutIfNeeded];

    heightTest=[self.tempUserCustomCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;


    return heightTest;



【讨论】:

谢谢,但我不确定你的意思。我没有设置任何固定的标签高度宽度(首选宽度设置为自动),原型单元格可以设置为默认值或任何自定义行高;结果还是一样。两个标签都将“Lines”设置为 0。我现在测试了在 heightForRowAtIndexPath 中的 tempCell 属性上设置文本和字体,并在返回高度之前执行 layoutIfNeeded,但现在我只得到一个截断的 titleLabel 而没有 messageLabel,无论如何我如何旋转设备。 此外,我的主要问题是我应该如何在 TableViewCell 中以编程方式设置两个自动调整多行标签,即使在构建IB 中的 TableViewCell。这两个选项显然被区别对待,到目前为止我还没有找到任何好的解释。 再次感谢,但本教程向我展示了我已经让它工作的情况,即使用“[Top,Leading,Trailing,Bottom] Space to Container”而不是“[顶部、前导、尾随、底部] 到容器边距的空间”。如何在 iOS 8 中以编程方式指定具有默认间距的“... Space to Container”?如果可以吗??

以上是关于UITableViewCell 中的几个多行标签,情节提要 xcode 6.3.2 中的自动布局的主要内容,如果未能解决你的问题,请参考以下文章

UITableViewCell 中的 UIStackView 与多行 UILabel

iOS 9 上的静态 UITableViewCell 中的多行 UILabel

UITableViewCell 内的多个堆栈视图内的多行标签

UITableViewCell 中带有多行标签的 UIStackView 高度不正确

UITableViewCell 中的多行 UILabel 在 iOS8 中使用 autoLayout 给出错误的高度

自定义 TableViewCell 中的标签不能是多行或换行