[英]TableView Images displays only when scrolled down
I am loading a tableview with images. 我正在加载带图像的tableview。 These are loaded asynchronously.
这些是异步加载的。 The images only get displayed with i scroll the tableview.
这些图像仅在我滚动tableview时显示。
I know this happens because of the reusable cells inside the tableview. 我知道发生这种情况是因为tableview中的可重用单元格。 My code is as follows.
我的代码如下。
How can i prevent this. 我怎样才能防止这种情况。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *c = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (c == nil)
{
c = [[MyCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"cell"];
}
NSURL *url = [NSURL URLWithString:@"http://www.ss.com/my.png"];
NSURLRequest *r = [NSURLRequest requestWithURL:url];
[c.myImageView setImageWithURLRequest:r placeholderImage:[UIImage imageNamed:@"def.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if (c)
{
c. myImageView.image = image;
[c setNeedsLayout];
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error: %@", error);
}];
In this case use lazy loading technique. 在这种情况下,请使用延迟加载技术。 As if we have to show the images in table view lazy loading is best technique.
好像我们必须在表视图中显示图像一样,延迟加载是最好的技术。 here is the library https://github.com/rs/SDWebImage Just add the SDWebImage folder in your project.
这是库https://github.com/rs/SDWebImage只需在项目中添加SDWebImage文件夹。 Then in your class import the following
然后在您的课程中导入以下内容
#import "UIImageView+WebCache.h"
and in cellforRowIndex method write the following code 并在cellforRowIndex方法中编写以下代码
NSURL *url = [NSURL URLWithString:@"http://www.ss.com/my.png"];
[c.myImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"def.png"]];
No need for success failure block as it will lazy load images and will keep them in cache by making as a key itself. 不需要成功失败块,因为它会延迟加载图像并通过将其作为密钥本身将其保留在缓存中。 So if the image is already loaded once then next time when you will open this controller, it will show up as if was in your local storage.
因此,如果映像已经加载一次,则下次打开该控制器时,它将显示为好像在本地存储中一样。 Thats the benefit of this library
那就是这个图书馆的好处
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.