2012-09-05 52 views
-1

我想在我的表格視圖中使用圖片延遲加載概念。我的image rul字符串數組是(MSMutableArray)「images」。我如何在cellForRowAtIndexPath中執行此操作。圖片在表格視圖中的延遲加載

它看起來像:

-(UITableViewCell *) tableView:(UITableView *)AtableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell=[searchtable dequeueReusableCellWithIdentifier:@"cell"]; 

    if(!cell) 
    { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 



     cell.textLabel.font=[UIFont systemFontOfSize:12]; 

     cell.imageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[appSmallLogos objectAtIndex:indexPath.row]]]]; 

     cell.detailTextLabel.text=[appReleaseDates objectAtIndex:indexPath.row]; 


    } 

    return cell; 
} 

回答

0

試試這個代碼:

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
indicator.frame = CGRectMake(your bounds); 
[cell addSubview:indicator]; 
[indicator bringSubviewToFront:self.view]; 
[indicator startAnimating]; 


     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^{ 
    //This is what you will load lazily 
     NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil]; 
     NSURL *imageURL=url; 
     NSData *image=[NSData dataWithContentsOfURL:imageURL]; 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
      CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; 
      cell.imageView.image=[UIImage imageWithData:image]; 
      [cell setNeedsLayout]; 
    [indicator stopAnimating]; 

     }); 
    }); 
cell.cellDescription.text=r.url;// this is which you want to add in first time. 

我不知道,但嘗試。

+0

非常感謝好友..它的工作正常。 :) – user1573162

+0

有一個快樂的編碼 –

+0

可以請你告訴我,如何顯示活動指標,直到圖像不顯示在表視圖單元格? – user1573162

0

可以使用SDWebImage庫,該庫將下載並緩存異步形象。通過網址傳遞給它作爲參數

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    // Here we use the new provided setImageWithURL: method to load the web image 
    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
        placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

    cell.textLabel.text = @"My Text"; 
    return cell; 
} 
+0

[cell.imageView setImageWithURL:[NSURL URLWithString:@「HTTP:// WWW 「.domain.com/path/to/image.jpg」] placeholderImage:[UIImage imageNamed:@「placeholder.png」]];此行顯示setImageWithUrl和佔位符圖像的錯誤。 – user1573162

+0

請確保您正確添加了庫到您的項目中,並且不要忘記'#import'UIImageView + WebCache.h''您想要顯示圖像的位置。 – janusbalatbat

+0

導入時顯示錯誤。文件存在的地方? – user1573162

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    TableCellLayout *cell = (TableCellLayout *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[TableCellLayout alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.cellName = [[array valueForKey:@"name"] objectAtIndex:indexPath.row]; 
    cell.cellImage = [UIImage imageNamed:@"placeholder.png"]; // add a placeholder  

    NSString *imageURL = [[array valueForKey:@"imageLink"] objectAtIndex:indexPath.row]; 
    NSURL *theURL = [NSURL URLWithString:imageURL]; 

    if (asynchronousImageLoader == nil){ 
     asynchronousImageLoader = [[AsynchronousImages alloc] init]; 
    } 
    [asynchronousImageLoader loadImageFromURL:theURL]; 

    cell.cellImage = asynchronousImageLoader.image; 

return cell; 
} 

我終於成功地做到這一點設置圖像用:

– performSelectorOnMainThread:withObject:waitUntilDone: 

後的圖像被下載

+0

我如何使用此代碼實現它? – user1573162