简体   繁体   English

UITableView-当它们离开屏幕时不要卸载单元格

[英]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.

相关问题 当在加载时不在屏幕上时,uitableview单元显示错误的数据 - uitableview cells display wrong data when off screen at loading time UITableView单元格不显示标签和按钮 - UITableView cells don't show labels and buttons 在屏幕关闭的情况下播放声音/不要让iPhone进入睡眠状态 - Play sound with screen turned off / don't let iPhone go to sleep 当没有更多要删除的单元格时,如何在 UITableView 中关闭编辑模式? - How to turn off editing mode in UITableView when there are no more cells to delete? UITableView 不要重复使用电池,这样做的好案例吗? - UITableView don't reuse cells, good case to do this? UITableView在滚动屏幕时清除单元格值 - UITableView clear the cell value when scroll off the screen 在UITableView中,为屏幕已关闭的单元格取消GCD操作的最佳方法是什么? - In a UITableView, best method to cancel GCD operations for cells that have gone off screen? 当弹出UINavigationController时,如何卸载UITabBarController? - How to unload a UITabBarController when popped off a UINavigationController? 看不到UITableView - Don't see UITableView UITableView dequeuReuseableCellWithIdentifier,当我不想显示图形时 - UITableView dequeuReuseableCellWithIdentifier, graphics appear when I don't want to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM