我有一個UICollectionView
實現與自定義UICollectionViewCell
。 數據的數組從互聯網獲取,但圖像在用戶訪問內容時下載。GCD和細胞重用UICollectionView
雖然這可能不是最有效的方法,我不打算這樣離開,但它確實暴露了我正在努力解決的某個問題。看起來這些單元格顯示「緩存」或「舊」圖像,當我緩慢滾動收集視圖時,單元格圖像不斷變化。
這可能是一個問題,實際的細胞圖像不可從網絡在那個時刻和我的問題是我如何強制一個空單元格或一些加載活性監視器,而不是顯示不正確的圖像?
在此先感謝,小
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Event";
EventCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *title = [[events objectAtIndex:indexPath.item] objectForKey:@"title"];
NSString *imageUrl = [[[events objectAtIndex:indexPath.item] objectForKey:@"photo"] objectForKey:@"url"];
dispatch_queue_t imageFetchQ = dispatch_queue_create("image fetched", NULL);
dispatch_async(imageFetchQ, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
if (imageData)
{
dispatch_async(dispatch_get_main_queue(), ^{
cell.eventImage.image = [UIImage imageWithData:imageData];
cell.eventTitle.text = title;
});
}
});
return cell;
}
我會建議你使用NSOperationQueue來代替,就像本教程中的內容: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues – 2013-04-29 08:12:04
我試過按照教程,但它並沒有真正處理重用問題。我看到NSOperationQueue將適用於這種解決方案,但問題依然存在。我想我需要實現某種機制來驗證哪個圖像即將顯示在每個單元格中。不知道如何。然而。 – Pinion 2013-05-02 23:23:43