[英]UITableView - don't unload cells when they go off screen
My tableView consists of 5-10 cells, each of which contains a scrollview of images. 我的tableView由5-10个单元格组成,每个单元格都包含图像的滚动视图。 The problem is that when a cell goes off screen, it removes all the images and then reloads them when the cell comes back on screen.
问题在于,当一个单元格退出屏幕时,它将删除所有图像,然后在该单元格返回屏幕时重新加载它们。 I know this is done for performance reasons, but in this case it just looks bad.
我知道这样做是出于性能方面的考虑,但是在这种情况下,它看起来很糟糕。 Is there a way to prevent the unloading of cells when they go off screen?
有什么方法可以防止细胞脱离屏幕时卸载?
Yes, of course. 当然是。 The general strategy is to preload all your
UITableViewCell
objects yourself. 一般的策略是自己预加载所有
UITableViewCell
对象。 You can do this in viewDidLoad
and unload them in viewDidUnload
, storing them in an NSMutableArray
, and then referencing the appropriate index in the array in your implementation of tableView:cellForRowAtIndexPath:
. 您可以在
viewDidLoad
执行此操作,然后在viewDidUnload
卸载它们,将它们存储在NSMutableArray
,然后在tableView:cellForRowAtIndexPath:
实现中引用数组中的适当索引。
You should have a pretty good reason for not doing things the Apple way. 您应该有充分的理由不采用Apple的方式行事。 With 5-10 cells, it doesn't sound like you have a good reason.
拥有5-10个单元格,听起来好像没有充分的理由。 Maybe you can improve the loading of cells using background threads in NSOperationQueue.
也许您可以使用NSOperationQueue中的后台线程来改善单元格的加载。
This post may be helpful: 这篇文章可能会有所帮助:
https://stackoverflow.com/questions/1408997/dynamic-uiimage-content-loading-in-uitableview-code https://stackoverflow.com/questions/1408997/dynamic-uiimage-content-loading-in-uitableview-code
Thanks for for the additional explanation. 感谢您的额外说明。 It was quite helpful.
这很有帮助。 By calculating the height if the uitableview based on the height of the uitaleviewcells, you may be able to avoid the problem you're having, without disabling reusable cells.
通过基于uitaleviewcells的高度计算uitableview的高度,您可以避免不存在的问题,而无需禁用可重复使用的单元格。 Additionally, if you are reloading cells from the network, you'll definitely benefit from some caching, as mentioned earlier.
另外,如果您要从网络中重新加载单元,则肯定会从某些缓存中受益,如前所述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.