UITableViewCell通过内容拥抱自动调整大小

格热哥兹·克鲁科夫斯基(Grzegorz Krukowski)

我要实现的是在其中包含UILabel的情况下自动调整单元格的大小。在开始拥抱部分之前,我一直能够使它工作。拥抱是指如果文本真的很短,则使UILabel尽可能小。

我准备了一个非常简单的例子来说明我的问题:

http://s000.tinyupload.com/index.php?file_id=11087347944522931899

当我将尾随约束设置为“ 500”优先级(低于然后是750)时,一切都很好。现在看来,尺寸调整单元正在减小UILabel的大小,但是从不增加,并且所有高度计算的确是错误的。

在第一个真正的短单元进入之后-在这个单元坏了之后所有的一切:

问题

From debugging I found out that once UILabel hits 8px, it will gets bigger anymore and all height calculation are done for 8px width.

I found a work around and resetting preferredWidth after cell is dequeued for whatever reason helps, but I'm not sure if there are any better ways of solving the problem.

My workaround is comments in attached project (line 55, line 81) ChatTableController.m and I'm looking for proper solution for that.

Rory McKinnel

Firstly, set the trailing constraint on your label to be >=20. This lets the label shrink.

Your real problem though is this method:

- (void)setBounds:(CGRect)bounds {
    [super setBounds:bounds];

    if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) {
        self.preferredMaxLayoutWidth = self.bounds.size.width;
        [self setNeedsUpdateConstraints];
    }
}

You would be better to set cell.theLabel.preferedMaxLayoutWidth when you create the cell in both heightForRowAtIndexPath and cellForRowAtIndexPath.

If you comment this code out completely it all seems to work.

You can also make your code work if you create a new prototype cell every time heightForRowAtIndexPath is called. This is what led me to the above function.

It seems that every time heightForRowAtIndexPath is called, the reuse of the prototype cell results in a combinatorial increase in height. You see this if you scroll down and back up.

So I would remove this method completely and let the constraints do the work for you. I see the cells shrink when one line to exact size and everything else compact.

Before you set the text in createCellForRowAtIndexPath add the following to make the layout width full screen minus your 20 margins:

cell.messageLabel.preferredMaxLayoutWidth=self.view.bounds.size.width-40;

and before you set the text in the prototype cells in heightForRowAtIndexPath:

prototypeCell.messageLabel.preferredMaxLayoutWidth=self.view.bounds.size.width-40;

设置preferredMaxLayoutWidth会导致旋转问题。设置每个单元格意味着每个单元格现在都具有特定于方向的约束。为了解决这个问题,您必须通过将以下内容添加到视图控制器中来在旋转时重新加载表:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self.tableView reloadData];
}

最后结果:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当内容更改时,如何告诉UITableViewCell“自动调整大小”?

来自分类Dev

UiTableViewCell为不同设备自动调整大小

来自分类Dev

如何使自动调整大小(高度)的UITableViewCell?

来自分类Dev

如何调整UITableViewCell的大小以适合其内容?

来自分类Dev

调整UITableViewCell的大小为其内容

来自分类Dev

如何通过新的自动调整大小功能和AutoLayout在iOS 8中获取UITableViewCell的高度?

来自分类Dev

iOS UITextField自动调整大小符合内容

来自分类Dev

如何包装textview自动调整大小的内容

来自分类Dev

qlineedit自动调整大小到内容

来自分类Dev

div大小自动调整为内容

来自分类Dev

TextArea根据内容自动调整大小

来自分类Dev

如何以编程方式使标签(NSTextField)通过内容自动调整大小?

来自分类Dev

通过拖放自动调整父容器的大小

来自分类Dev

如何根据UITextView的内容调整UITextView和UITableViewCell的大小

来自分类Dev

重新打开IB后,UITableViewCell的内容将调整大小

来自分类Dev

调整UITableViewCell高度的大小

来自分类Dev

调整大小的 UITableViewCell 变小

来自分类Dev

根据内容自动调整文本区域的大小

来自分类Dev

根据窗口内容自动调整窗口大小

来自分类Dev

Excel中基于列内容的自动调整列大小

来自分类Dev

JavaFX GridPane单元无法自动调整内容大小

来自分类Dev

如何根据QScrollArea中的子内容自动调整QVBoxLayout的大小?

来自分类Dev

找不到自动调整iframe大小以适应其内容的方法

来自分类Dev

如何根据其内容自动调整NSTextView的大小?

来自分类Dev

如何自动调整div元素的大小以适合内容

来自分类Dev

如果调整页面大小,则左侧菜单有拥抱间隙

来自分类Dev

UIView居中并通过约束自动布局调整大小

来自分类Dev

键入时调整UITextField的大小(通过使用自动布局)

来自分类Dev

如何通过纯 JavaScript 自动调整博主图片的大小?

Related 相关文章

  1. 1

    当内容更改时,如何告诉UITableViewCell“自动调整大小”?

  2. 2

    UiTableViewCell为不同设备自动调整大小

  3. 3

    如何使自动调整大小(高度)的UITableViewCell?

  4. 4

    如何调整UITableViewCell的大小以适合其内容?

  5. 5

    调整UITableViewCell的大小为其内容

  6. 6

    如何通过新的自动调整大小功能和AutoLayout在iOS 8中获取UITableViewCell的高度?

  7. 7

    iOS UITextField自动调整大小符合内容

  8. 8

    如何包装textview自动调整大小的内容

  9. 9

    qlineedit自动调整大小到内容

  10. 10

    div大小自动调整为内容

  11. 11

    TextArea根据内容自动调整大小

  12. 12

    如何以编程方式使标签(NSTextField)通过内容自动调整大小?

  13. 13

    通过拖放自动调整父容器的大小

  14. 14

    如何根据UITextView的内容调整UITextView和UITableViewCell的大小

  15. 15

    重新打开IB后,UITableViewCell的内容将调整大小

  16. 16

    调整UITableViewCell高度的大小

  17. 17

    调整大小的 UITableViewCell 变小

  18. 18

    根据内容自动调整文本区域的大小

  19. 19

    根据窗口内容自动调整窗口大小

  20. 20

    Excel中基于列内容的自动调整列大小

  21. 21

    JavaFX GridPane单元无法自动调整内容大小

  22. 22

    如何根据QScrollArea中的子内容自动调整QVBoxLayout的大小?

  23. 23

    找不到自动调整iframe大小以适应其内容的方法

  24. 24

    如何根据其内容自动调整NSTextView的大小?

  25. 25

    如何自动调整div元素的大小以适合内容

  26. 26

    如果调整页面大小,则左侧菜单有拥抱间隙

  27. 27

    UIView居中并通过约束自动布局调整大小

  28. 28

    键入时调整UITextField的大小(通过使用自动布局)

  29. 29

    如何通过纯 JavaScript 自动调整博主图片的大小?

热门标签

归档