IOS - 自我调整细胞问题 [英] IOS - self sizing cells issue

查看:135
本文介绍了IOS - 自我调整细胞问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临使用自动布局自动调整单元格大小的问题。我的目标是实现一个看起来像这样的表:

I am facing an issue with auto sizing cells using Auto Layout. My goal is to achieve a table that will look something like this:

| Title_label (time)      $price |
|                                |
|Some long description. More     |
|description.                    |
|                                |

当标题很长时,它应如下所示:

When the title is long it should look like this:

| This title is (time)    $price |
| really long                    |
|                                |
|Some long description. More     |
|description.                    |
|                                |

因此,当标题变大时,只要有8个点空间beetwen,它就会将时间标签向右推时间和价格。如果它更大,它应该换行到下一行。

So when title gets bigger it pushes time label to the right as long as there is 8 points space beetwen time and price. If it is even bigger it should wrap to next line.

我在表格视图之前用自定尺寸单元格做了但只有一个扩展标签,而不是两个。

I have done before table view with self sizing cells but there where only one expanding label, not two.

我已经实现了行的自动高度:

I have implemented row's automatic height:

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100

这就是我的约束的样子: / p>

This is how my constraints look like:

|     8px
|8px title 8px time >=8px       price|
|     8px                            |
|8px description                  8px|
|     8px                            |

价格时间和头衔之间也存在最高限价。

There is also top alignment between price time and title.

我将标题和描述的行数设置为0.
我将时间和价格的抗压力设置为1000(因为标题与它们重叠)。

I've set lines number of title and description to 0. I've set compression resistance of time and price to 1000 (because title was overlapping them).

但是标题标签不会换行到下一行。它结束于....更多描述标签也太小了。当我滚动表时desription的高度是固定的。

However the title label doesn't wrap to next line. It ends with .... What is more description label also is too small. When I scroll the table desription's height is fixed.

我尝试在返回cell之前添加cell.layoutIfNeeded()。然后单元格布局搞砸了(标题被剪裁)但是当我滚动tV时一切正常。

I've tried adding cell.layoutIfNeeded() before returning cell. Then cell layout gets messed up (title is clipped) but when I scroll tV everything is OK.

任何想法?

编辑:
这是因为标题标签位于其他标签旁边,它不知道何时应该换行?

Is this because title label is next to other labels and it doesn't know when it should wrap?

我试过

override func layoutSubviews() {
        self.nameLabel.preferredMaxLayoutWidth -= (durationLabel.frame.width + priceLabel.frame.width + 16)
        super.layoutSubviews()
    }

告诉标题标签什么是它的最大宽度,但它会让事情变得混乱。

to tell title label what is its max width but it messes things up.

推荐答案

尝试添加

[cell layoutIfNeeded]

这篇关于IOS - 自我调整细胞问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆