I have a UITableview which showing Facebook news feed. There are some images in each news feed with different size. So i have to load these images lazily and at same time , i have to set the cell height according to the image height(which are not loaded when callling heightForrowAtIndexPath).
我有一个UITableview显示Facebook的新闻提要。每个新闻提要中都有不同大小的图像。因此,我必须延迟加载这些图像,同时,我必须根据图像的高度来设置单元格的高度(当callling heightForrowAtIndexPath时,它没有加载)。
The problem is i don't have the image width and height if its not downloaded.So how to set cell height according to the images in that cell ? I googled a a lot .Lazy loading examples showing only images with fixed size.Please help me..Thanks in adavence.
问题是如果没有下载,我就没有图像的宽度和高度。那么如何根据单元格中的图像设置单元格高度呢?我在谷歌上搜索了很多。懒惰的加载例子只显示了固定大小的图像。请帮我. .由于adavence。
3 个解决方案
#1
1
Well you can't know the size of the image before you download it. You can check in the heightForCellAtIndexPath
if the image is downloaded. Then set the correct height, just call beginUpdate
and endUpdate
on your table view after you downloaded an image.
在下载之前你不可能知道图像的大小。如果图像被下载,您可以在heightForCellAtIndexPath中进行检查。然后设置正确的高度,在你下载了一个图片后,只需在你的表格视图上调用beginUpdate和136。
#2
1
You need to load images in background thread initially don't show any images then from background load these images as fast as your image is load, reload the table form the background then set a bool variable for identifying that image is loaded.
您需要在后台线程中加载图像,先不要显示任何图像,然后从背景加载这些图像的速度和图像的加载速度一样快,然后重新加载表格的背景,然后设置一个bool变量来确定图像是否被加载。
And adjust the height accordingly, also you need to fix a size of imageView where you are going to show the images. No need to make it big or small. make it fixed(suppose 40*40). Then you can adjust your cell.
然后相应地调整高度,你还需要固定一个imageView的大小在你要显示图像的地方。没有必要把它做大或小。使它固定(假设40 * 40)。然后你可以调整你的单元格。
For reloading the table from background use this line of code in you background selector
要从后台重新加载表,请在后台选择器中使用这一行代码
[self.yourTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
#3
0
Find below very good apple official example.
以下是很好的苹果官方例子。
LazyTableImages
From Apple doc.
从苹果医生。
This sample demonstrates a multi-stage approach to loading and displaying a UITableView. It begins by loading the relevant text from an RSS feed so the table can load as quickly as possible, and then downloads the images for each row asynchronously so the UI is more responsive.
这个示例演示了加载和显示UITableView的多阶段方法。它首先从RSS提要加载相关的文本,以便表能够尽快加载,然后异步地下载每一行的图像,以便UI响应更快。
#1
1
Well you can't know the size of the image before you download it. You can check in the heightForCellAtIndexPath
if the image is downloaded. Then set the correct height, just call beginUpdate
and endUpdate
on your table view after you downloaded an image.
在下载之前你不可能知道图像的大小。如果图像被下载,您可以在heightForCellAtIndexPath中进行检查。然后设置正确的高度,在你下载了一个图片后,只需在你的表格视图上调用beginUpdate和136。
#2
1
You need to load images in background thread initially don't show any images then from background load these images as fast as your image is load, reload the table form the background then set a bool variable for identifying that image is loaded.
您需要在后台线程中加载图像,先不要显示任何图像,然后从背景加载这些图像的速度和图像的加载速度一样快,然后重新加载表格的背景,然后设置一个bool变量来确定图像是否被加载。
And adjust the height accordingly, also you need to fix a size of imageView where you are going to show the images. No need to make it big or small. make it fixed(suppose 40*40). Then you can adjust your cell.
然后相应地调整高度,你还需要固定一个imageView的大小在你要显示图像的地方。没有必要把它做大或小。使它固定(假设40 * 40)。然后你可以调整你的单元格。
For reloading the table from background use this line of code in you background selector
要从后台重新加载表,请在后台选择器中使用这一行代码
[self.yourTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
#3
0
Find below very good apple official example.
以下是很好的苹果官方例子。
LazyTableImages
From Apple doc.
从苹果医生。
This sample demonstrates a multi-stage approach to loading and displaying a UITableView. It begins by loading the relevant text from an RSS feed so the table can load as quickly as possible, and then downloads the images for each row asynchronously so the UI is more responsive.
这个示例演示了加载和显示UITableView的多阶段方法。它首先从RSS提要加载相关的文本,以便表能够尽快加载,然后异步地下载每一行的图像,以便UI响应更快。