2014-05-12 167 views
1

我的問題是----我在custom cell中使用UITableView當加載數據在tableview它是加載描述和標題也日期但沒有加載圖像tableview.some次加載所有圖像在一行。當我使用此代碼滾動工作正常,但不加載圖像在TableView。我想要延遲加載概念。去下面的鏈接。在tableview中延遲加載圖像

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    newsobject=(newscustamCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (newsobject ==nil) 
    { 
     [[NSBundle mainBundle]loadNibNamed:@"newscustamCell" owner:self options:nil]; 
    } 
    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(q, ^{ 

     NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]; 

     /* Fetch the image from the server... */ 

     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *img = [[UIImage alloc] initWithData:data]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      /* This is the main thread again, where we set the tableView's image to 
     be what we just fetched. */ 

     newsobject.allnewsimg.image = img; 
    // newsobject.allnewsimg.image=[UIImage imageWithData:data]; 

     }); 

    }); 

     newsobject.allnewsdes.text=[[[newsarry objectAtIndex:indexPath.row]   objectForKey:@"long_desc"]stringByStrippingTags]; 

     newsobject.allnewslabel.text=[[newsarry objectAtIndex:indexPath.row]  objectForKey:@"title"]; 
    ![enter image description here][1] 
     newsobject.timelabel.text=[[newsarry objectAtIndex:indexPath.row] objectForKey:@"date"]; 

    return newsobject; 
    } 

回答

0

我將分享你一些代碼。請遵循此代碼。

首先,您必須從服務器檢索所有數據。之後在tableview加載。

試試這個代碼:

- (void)viewDidLoad 
{  
    NSString *[email protected]"http://put your url"; 
    self.responseData=[NSMutableData data]; 
    NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self]; 
    NSLog(@"connection====%@",connection); 
} 

JSON委託方法-------------

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [self.responseData setLength:0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.responseData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    self.responseData=nil; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSArray *response_Array = [NSJSONSerialization JSONObjectWithData:self.responseRankData options:kNilOptions error:nil]; 

    NSDictionary *dataDictionary=nil; 
    NSDictionary *imgurlDic=nil; 
    NSArray *imgDic=nil; 

    // I am retrieve image according to my url. So please edit this according to your url. i am just write the format. 

    for (int i=0; i < response_Array.count; i++) 
    { 
     imgurlDic = [imgDic objectAtIndex:i]; 
     imgDic = [dataDictionary objectForKey:@"imageKey"]; // Take your Key according to URL 

     NSURL *url = [NSURL URLWithString:[imgurlDic objectForKey:@"url"]]; 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *img = [[UIImage alloc] initWithData:data]; 
     [self.Img_Array addObject:img]; // Adding all image in your image Array. 

     NSLog(@"imgurlDic == %@",imgurlDic); 
    } 

    [self.tableview reloadData]; // reload your tableview here. 
} 

讓所有圖像陣列中,你可以使用這個數組在您的cellForRowAtIndexPath方法或您想要加載圖像的位置。

+1

的NSData *數據= [NSData的dataWithContentsOfURL:URL];如果圖像數據較大,它將阻止用戶界面。它不是懶加載的概念。 – Pawan

+0

我從URL檢索480x1024大小的圖像,並顯示在我的tableview單元格,其中frameize只有40x40。所以我認爲會的。 –

+0

問題是關於延遲加載的概念,你可以用後臺線程做同樣的事情。這並不會阻止你的用戶界面。 – Pawan

1

您可以使用一些LibSDWebImageAfnetworkingLibs支持延遲加載圖像並自動緩存此圖像。 所有你需要做的:

[cell.imageView setImageWithURL:[NSURL URLWithString:@"url of image"] 
        placeholderImage:nil]; 
+0

謝謝你回答我,我已經這樣做,我從服務器獲取圖像,但滾動非常緩慢,它的擊中。 – venky

+0

嗨,我看到你的例子程序,但它的工作給我,但滾動非常緩慢,並轉移到結果。 – venky

+0

我從服務器獲取所有數據,並將對象添加到newsarray對象的newsarray數據中。當我運行這個程序所有數據dislay tablview(圖像和長描述和數據和標題也)但當我滾動tablview它非常緩慢,並移動到結果因爲圖像加載lazy.how我可以解決我的問題... – venky

0

的因爲該表視圖的細胞迅速排隊的方法/再利用,通過你的網絡的要求完成加載,你指的是電池的時間可能是不一樣的一個你認爲它是(因爲它離開屏幕並被排隊,然後重新分配到不同的indexPath)。此外,在返回單元格之前,您需要確保將imageViewimage屬性重置爲nil或某個默認值。請參閱我的答案以獲取更多信息:https://stackoverflow.com/a/23473911/91458

此外 - 最好保留已檢索圖像的緩存。檢查類NSCache類,這是非常類似於使用可變字典,但具有內置內存管理,等等。

0

我也使用了這個技巧,但線程和索引之間的線程弄得亂七八糟,所以我搜索,我發現GitHub上的一個偉大的圖書館。

你只需要#import <SDWebImage/UIImageView+WebCache.h>到你的項目,你也可以定義佔位符,當圖像被與眼前這個代碼下載:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString *CellIdentifier; 
     CustomCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     MyObject *object = (MyObject *)[self.list.array objectAtIndex:indexPath.row]; 

     [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

     return cell; 
} 

它還高速緩存下載圖像,並給你很好的表現。

希望它能幫助你!

+0

嗨,我下載並傳播此框架,但我得到了一個錯誤。即找不到#import 文件。 – venky

+0

@ user3627091你是通過Cocoa Pods來安裝它,還是你手動將文件夾複製到你的項目中。 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @「Cell」;如果您執行了第二個操作,則需要導入UIImageView + WebCache.h – EridB

0

我希望這個鏈接對你有幫助。通過使用異步類圖像加載異步...

https://github.com/nicklockwood/AsyncImageView

+0

- (UITableViewCell *)。 newsobject =(newscustamCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; – venky

+0

if(newsobject == nil) {[NSBundle mainBundle] loadNibNamed:@「newscustamCell」owner:self options:nil];您可以使用下面的代碼來創建一個新的URL:{「newsarry objectAtIndex:indexPath.row」objectForKey:@「image_file」]]]; – venky

+0

NSData * data = [NSData dataWithContentsOfURL:url]; newsobject.allnewsimg.image = [UIImage imageWithData:data]; newsobject.allnewsdes.text = [[[newsarry objectAtIndex:indexPath.row] objectForKey:@「long_desc」] stringByStrippingTags]; – venky

4
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
      dispatch_async(queue, ^{ 

       NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]; 

       NSData *data = [NSData dataWithContentsOfURL:url]; 
       UIImage *image = [UIImage imageWithData:data]; 

       dispatch_async(dispatch_get_main_queue(), ^{ 

      [tableView cellForRowAtIndexPath:indexPath].imageView.image = image; 


     }); 
    }); 
+0

當我使用這些代碼我可以解決圖像懶加載問題... – venky