iOS8改变动态单元“现在”的高度,重新设置内部单元格内容 [英] iOS8 change height of dynamic cell "now", re internal cell content

查看:167
本文介绍了iOS8改变动态单元“现在”的高度,重新设置内部单元格内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个动态单元格

注意 - 在示例中,文本不是数据驱动的。它只是单元格的一些文本(例如,考虑帮助文本)。在运行时,使用单元格内的实际按钮,将UILabel的.text从一个单词更改为多行。 iOS正好调整单元格和表格的大小....

Note - in the example, the text is not data driven. It's just some text local to the cell (consider, say, a help text). At runtime, change the .text of the UILabel from one word to many lines, using a button actually inside the cell. iOS perectly resizes the cell and table....

...但仅当单元格在屏幕外滚动,然后再次打开时。

如何提醒表视图重新计算现在的所有内容?

How to alert the table view to recalculate everything "now" ?

(请注意,此问题仅适用于iOS8 +,Xcode7 +,动态单元格高度的自动布局。)

推荐答案

我假设您没有设置 UILabel text 属性 cellForRowAtIndexPath ,而是在其他地方(或异步执行)。如果是这种情况,我不会在那里更新UI。相反,我会更新支持表的模型,然后调用 reloadRowsAtIndexPaths 。这将再次让 cellForRowAtIndexPath 调用,但与重新加载整个表不同,这将优雅地保持tableview的 contentOffset 它在哪里。

I'm presuming you're not setting the text property of the UILabel inside cellForRowAtIndexPath but rather somewhere else (or doing it asynchronously). If that's the case, I wouldn't update the UI there. Rather, I'd update the model backing the table and then call reloadRowsAtIndexPaths. That will let cellForRowAtIndexPath call again, but unlike reloading the whole table, this will gracefully keep the contentOffset of the tableview right where it is.

我知道这一切听起来都不必要复杂,但最重要的是你不拥有这个视图,表视图也是如此。除了更新单元格之外,它还必须做各种各样的事情。即,如果细胞生长,找出哪些细胞滚出视野并使它们出列。如果单元格收缩,请确定哪些单元格作为结果滚动到视图中。

I know this all sounds unnecessarily complicated, but the bottom line is that you don't own this view, the table view does. It has to do all sorts of stuff above and beyond updating the cell. I.e., if the cell grew, figure out which cells scrolled out of view and dequeue them. If the cell shrunk, figure out which cells scrolled into view as a result.

这是一个非常复杂的舞蹈。您可以尝试在单元格上调用 setNeedsLayout ,但我不希望这样做(即使它确实如此,它也是一种脆弱的方法)。表视图负责管理其单元格,所以如果你真的应该更新模型并重新加载那个单元格。

It's a surprisingly complex dance. You can try calling setNeedsLayout on the cell, but I wouldn't expect that to work (and even if it does, it is a fragile approach). The table view is responsible for managing its cells, so if you really should just update model and reload that one cell.

这篇关于iOS8改变动态单元“现在”的高度,重新设置内部单元格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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