有没有办法更新单个UITableViewCell的高度,而无需重新计算每个单元格的高度? [英] Is there a way to update the height of a single UITableViewCell, without recalculating the height for every cell?

查看:329
本文介绍了有没有办法更新单个UITableViewCell的高度,而无需重新计算每个单元格的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个不同部分的UITableView。一个部分包含将在用户将文本键入UITextView时调整大小的单元格。另一部分包含呈现HTML内容的单元格,计算高度相对较贵。

I have a UITableView with a few different sections. One section contains cells that will resize as a user types text into a UITextView. Another section contains cells that render HTML content, for which calculating the height is relatively expensive.

现在当用户输入UITextView时,为了获得表格视图为了更新单元格的高度,我调用

Right now when the user types into the UITextView, in order to get the table view to update the height of the cell, I call

[self.tableView beginUpdates];
[self.tableView endUpdates];

但是,这会导致表重新计算每个单元格的高度该表,当我真的只需要更新输入的单个单元格时。不仅如此,而不是使用 tableView:estimatedHeightForRowAtIndexPath:重新计算估计的高度,它调用 tableView:heightForRowAtIndexPath: for每个单元格,甚至是那些没有显示的单元格。

However, this causes the table to recalculate the height of every cell in the table, when I really only need to update the single cell that was typed into. Not only that, but instead of recalculating the estimated height using tableView:estimatedHeightForRowAtIndexPath:, it calls tableView:heightForRowAtIndexPath: for every cell, even those not being displayed.

有没有办法让表视图更新单个单元格的高度,而不需要完成所有这些不必要的工作?

Is there any way to ask the table view to update just the height of a single cell, without doing all of this unnecessary work?

更新

我仍在寻找解决方案。正如所建议的那样,我尝试使用 reloadRowsAtIndexPaths:,但它看起来不会起作用。即使只有一行调用 reloadRowsAtIndexPaths:仍然会导致每行调用 heightForRowAtIndexPath:,即使 cellForRowAtIndexPath:只会为您请求的行调用。实际上,看起来每次插入,删除或重新加载行时,都会为表格单元格中的每一行调用 heightForRowAtIndexPath:

I'm still looking for a solution to this. As suggested, I've tried using reloadRowsAtIndexPaths:, but it doesn't look like this will work. Calling reloadRowsAtIndexPaths: with even a single row will still cause heightForRowAtIndexPath: to be called for every row, even though cellForRowAtIndexPath: will only be called for the row you requested. In fact, it looks like any time a row is inserted, deleted, or reloaded, heightForRowAtIndexPath: is called for every row in the table cell.

我也尝试将代码放在 willDisplayCell:forRowAtIndexPath:中来计算单元格出现之前的高度。为了使其工作,我需要强制表视图在我进行计算后重新请求行的高度。不幸的是,调用 [self.tableView beginUpdates]; [self.tableView endUpdates]; 来自 willDisplayCell:forRowAtIndexPath:导致UITableView内部代码中的索引超出范围异常。我猜他们不希望我们这样做。

I've also tried putting code in willDisplayCell:forRowAtIndexPath: to calculate the height just before a cell is going to appear. In order for this to work, I would need to force the table view to re-request the height for the row after I do the calculation. Unfortunately, calling [self.tableView beginUpdates]; [self.tableView endUpdates]; from willDisplayCell:forRowAtIndexPath: causes an index out of bounds exception deep in UITableView's internal code. I guess they don't expect us to do this.

我不禁觉得这是SDK中的一个错误,以回应 [self.tableView endUpdates] 对于不可见的单元格,它不会调用 estimatedHeightForRowAtIndexPath:但我仍在尝试查找某种解决方法。感谢任何帮助。

I can't help but feel like it's a bug in the SDK that in response to [self.tableView endUpdates] it doesn't call estimatedHeightForRowAtIndexPath: for cells that aren't visible, but I'm still trying to find some kind of workaround. Any help is appreciated.

推荐答案

此错误已在iOS 7.1中修复。

This bug has been fixed in iOS 7.1.

在iOS 7.0中,似乎没有办法解决这个问题。调用 [self.tableView endUpdates] 会导致 heightForRowAtIndexPath:为表格中的每个单元格调用。

In iOS 7.0, there doesn't seem to be any way around this problem. Calling [self.tableView endUpdates] causes heightForRowAtIndexPath: to be called for every cell in the table.

但是,在iOS 7.1中,调用 [self.tableView endUpdates] 会导致 heightForRowAtIndexPath:,并为非可见单元格调用 estimatedHeightForRowAtIndexPath:

However, in iOS 7.1, calling [self.tableView endUpdates] causes heightForRowAtIndexPath: to be called for visible cells, and estimatedHeightForRowAtIndexPath: to be called for non-visible cells.

这篇关于有没有办法更新单个UITableViewCell的高度,而无需重新计算每个单元格的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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