简体   繁体   English

TableView Images仅在向下滚动时显示

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

相关问题 每次滚动tableview时,单元格中的图像都会改变 - Images in cell are changing everytime when tableview is scrolled 在表格视图中滚动时图像重复单元格 - images repeating cells when scrolled in tableview 向下滚动时,UICollectionView中的图像会自动翻转? - Images in UICollectionView automatically flips when scrolled down? 向下滚动时如何使UIButton在表格视图中保持选中状态 - How to make UIButton stay selected in a tableview when scrolled down 自定义 TableView 中的 UIView 背景颜色仅在滚动时更改,为什么? - UIView background color in Custom TableView only change when scrolled, why? 在开始时将TableView嵌入ContainerView中向下滚动 - Embed TableView In ContainerView is scrolled down at start 仅当 UIWebView 向下滚动一点时,状态栏才会检测到点击 - Status bar detects taps only when the UIWebView is scrolled down a bit 在仅向上滚动TableView时显示的UITableView上方添加隐藏视图 - Add a hidden view above a UITableView that is only displayed when TableView is scrolled up Tableview滚动时不会加载更多数据 - Tableview not Load More Data When It Scrolled 自定义UITableViewCell在滚动TableView时发生更改 - Custom UITableViewCell changes when TableView scrolled
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM