如何告诉我的UITableViewCell“自动调整大小”?当它的内容发生变化? [英] How do I tell my UITableViewCell to "auto-resize" when its content changes?

查看:67
本文介绍了如何告诉我的UITableViewCell“自动调整大小”?当它的内容发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的cell.xib中,我有一个标签,其所有方面都有约束。我已将该标签设置为 lines = 0 line-break =自动换行。然后,我对我的TableView执行此操作:

In my cell.xib, I have a label, with constraints to all its sides. I've set that label to lines = 0 and line-break = word wrap. Then, I do this to my TableView:

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100.0

一切都很好,我的UITableViewCell是自动高度。如果文本很长,那么我的tableView会智能地计算大小。

Everything works great, and my UITableViewCell is auto-height. If the text is long, then my tableView intelligently calculates the size.

问题是 - 我怎么告诉我的UITableView一旦重新计算大小单元格中的内容更改?

The problem is -- how do I tell my UITableView to "re-calculate" the size once the content changes in the cell?

我的单元格可以调用它的委托,在这个委托中,我希望TableView重新绘制高度。

My cell could call its delegate, and in this delegate, I'd like the TableView to re-draw the height.

现在,我的单元格中的内容不断变化,但单元格高度永远不会改变。

Right now, the content in my cells change constantly, but the cell height never changes.

推荐答案

有记录的方法可以做到这一点。请参阅 UITableView.beginUpdates() 文档

There is a documented way to do this. See UITableView.beginUpdates() documentation:


您也可以使用此方法,然后使用endUpdates方法在不重新加载单元格的情况下对行高的更改进行动画处理。

You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

因此,正确的解决方案是:

So, the correct solution is:

tableView.beginUpdates()
tableView.endUpdates()

另请注意,有一项功能没有记录 - 您也可以在此处为更新动画添加完成处理程序:

Also note that there is a feature that is not documented - you can add a completion handler for the update animation here, too:

tableView.beginUpdates()
CATransaction.setCompletionBlock {
   // this will be called when the update animation ends
}

tableView.endUpdates()

然而,轻描淡写,它没有记录(但它的工作原理是因为 UITableVie w 为动画使用 CATransaction

However, tread lightly, it's not documented (but it works because UITableView uses a CATransaction for the animation).

这篇关于如何告诉我的UITableViewCell“自动调整大小”?当它的内容发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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