iPhone:UITableView重新加载数据计时问题
我遇到了常见的 TableView 数据重新加载问题。我读过很多关于同一主题的问题,但问题从来都与我的问题不完全相同......
我有一个基于导航的应用程序。在 RootViewController 的 viewDidLoad 方法中,我发出请求以获取 JSON 数据(文章)。连接加载完成后,我为每个条目创建自定义 Article 对象。 Article 类有一个 initWithDictionnary 方法,该方法初始化对象的属性,最重要的是创建下载图像的请求。连接加载完成后,我设置了 Article 对象的图像属性。 目标是使用下载的图像初始化 cell.imageView.image 属性。此时,您可能已经猜到问题所在了。 在渲染单元格图像后下载图像,因此它保持为空,直到重新加载单元格(例如,如果它离开屏幕然后返回)。 我想我应该在某个时间调用 reloadData 但我不知道什么时候。理想情况下,我会在加载所有单元格时调用它,但这似乎不可能。 我尝试过很多疯狂的事情,例如等待单元格加载并尝试重新加载前一个单元格,但它不起作用。 通过阅读这里的问答,我了解了 Apple 的 LazyTableImage 示例代码,但我不理解所有内容,所以我不确定我应该/可以使用它。
如果您需要更多详细信息,请询问我。
预先感谢您的任何帮助。
I'm encountering a common TableView data reloading problem. I've read many questions on the same subject but the problem was never exactly the same as mine...
I have a navigation based application. In the RootViewController's viewDidLoad method I make a request in order to get JSON data (articles). When the connection has finished loading I create custom Article objects for each entry. The Article class has an initWithDictionnary method which initializes the attributes of the object and most importantly creates a request to download an image. When the connection has finished loading I set the image attribute of the Article object.
The goal is to initialize the cell.imageView.image property with the downloaded image. At that point, you may have guessed what the problem is about.
Images are downloaded after the cell image is rendered so it stays empty until the cell is reloaded (if it gets out of the screen and then back in for example).
I guess I should call reloadData at a certain time but I don't know when. Ideally I would call it when all the cells are loaded but it doesn't seem possible.
I've tried a bunch of crazy things like waiting for a cell to load and the try to reload the previous one but it didn't work.
By reading Q&A out here I learned about Apple's LazyTableImage sample code but I don't understand all of it so I'm not sure I should/could use it.
Please ask me if you need more details.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须异步加载图像。请记住,您的表数据每行应包含一个字段,指定图像是否已加载,以便(如果为 true)您可以跳过加载并仅显示缓存的图像。
首先看这个问题:异步加载图像
You have to load the images Asynchronously. Keep in mind that your table data should contain a field per row specifying if the image was loaded so that (if true) you can skip loading and just display cached images.
Look at this question first: Load images async
大约有 10 个优秀的 UITableView 延迟加载图像教程。选择一个。
一段时间后,您将通过查看教程来掌握它的窍门。坚持下去,这是一个重要的概念,不仅对于这个项目,而且对于你的其余编程生涯也是如此。
延迟加载图像 UITableView 教程这会让你变得更聪明!
There are about 10 good Lazy Loading Image UITableView tutorials. Pick one.
You'll get the hang of it by reviewing the tutorials after a while. Stick with it, it's an important concept, not only for this project, but for the rest of your programming career.
Lazy Loading Image UITableView Tutorials that'll make you smarter!