簡體   English   中英

TableView Images僅在向下滾動時顯示

[英]TableView Images displays only when scrolled down

我正在加載帶圖像的tableview。 這些是異步加載的。 這些圖像僅在我滾動tableview時顯示。

我知道發生這種情況是因為tableview中的可重用單元格。 我的代碼如下。

我怎樣才能防止這種情況。

- (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);

    }];

在這種情況下,請使用延遲加載技術。 好像我們必須在表視圖中顯示圖像一樣,延遲加載是最好的技術。 這是庫https://github.com/rs/SDWebImage只需在項目中添加SDWebImage文件夾。 然后在您的課程中導入以下內容

#import "UIImageView+WebCache.h"

並在cellforRowIndex方法中編寫以下代碼

 NSURL *url = [NSURL URLWithString:@"http://www.ss.com/my.png"];
[c.myImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"def.png"]];

不需要成功失敗塊,因為它會延遲加載圖像並通過將其作為密鑰本身將其保留在緩存中。 因此,如果映像已經加載一次,則下次打開該控制器時,它將顯示為好像在本地存儲中一樣。 那就是這個圖書館的好處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM