2012-05-15 61 views
0

我正在嘗試異步加載tableViewCell中的圖像。 當我上下移動表格時,圖像顯示不然。異步映像加載錯誤

AsyncImageView包含NSUrlConnection。它不會進入委託「connectionDidFinishLoading」。但是當我搬桌子向上或向下然後它這個委託功能

下面內部代碼

 CGRect frame; 
     frame.size.width=115; frame.size.height=100; 
     frame.origin.x=0; frame.origin.y=0; 
     AsyncImageView* asyncImage = [[[AsyncImageView alloc] 
             initWithFrame:frame] autorelease]; 
     loadingTable=[[UIActivityIndicatorView alloc] initWithFrame:frame]; 
     [cell.contentView addSubview:loadingTable]; 
     [loadingTable startAnimating]; 
     asyncImage.tag = 999; 
     NSURL* url = [NSURL URLWithString:[[videoCollection objectAtIndex:indexPath.row] videoImageUrl] ]; 
     [asyncImage loadImageFromURL:url activity:loadingTable]; 
     [cell.contentView addSubview:asyncImage]; 

回答

1

第一負載的所有圖像,並將它們添加一些數據結構,像數組之後,它展示給表。不要與網絡電話的UI重疊

+0

但當它向上或向下移動表格時正確顯示圖像 –

+0

然後你的代表沒有正確設置 – Saad

+0

調用[tablview reloadData]; – Saad

0

我敢肯定,如你所知,所有與用戶界面相關的業務都必須在主線程上完成。但是,下載大量數據可以並且應該在後臺完成。而這個Grand Central Dispatch的tutorial可能正是你需要的。

在後臺線程中下載圖像,並在完成後,讓主線程知道這一點,並在主線程上更新UI。

0

我昨天做過這件事。

最好的方法是在後臺和 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中調用下載。

當用戶想要看到它時,此方法將加載單元格的圖像。

但警告!爲了防止多次下載相同的圖像,您應該使用數據結構(如字典)來檢查圖像是否已下載。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    ... 
    if([myPictures objectForKey:indexPath.row] == nil) { 
     [self performSelectorInBackground:@selector(downloadPhotoInBackground:) withObject:indexPath]; 
    } 
    return cell 
} 

和downloadPhotoInBackground:,不要忘記添加您的下載圖像到「myPictures」屬性。