2014-05-15 16 views
0

UPDATE:的iOS更新UICollectionViewCell的ImageView在後臺線程

通過設置單元格的文本和拇指的ImageView爲默認值,所有細胞從空白單元格,而不是顯示的東西只是顯示然後更新到正確的東西加載。

BUT細胞尚未重新使用。我的意思是如果我向上滾動隱藏一些單元格然後向下滾動,所有已顯示的單元格將被重新加載而不是立即顯示。我發現UICollectionView do not reuse cells但沒有工作。


這是一個基於UIDocument的手寫筆記本應用程序。每個音符都有大量數據,所以我使用NSFileWrapper來加載標題thumb當所有音符都顯示在UICollectionView中時。

問題是:啓動後一切正常,每個單元的拇指圖像從背景線程[UIDocument openWithCompletionHandler]中讀取。然後我向上滾動,所有新細胞得到「髒」拇指完全相同,只是向上滾動的細胞,然後立即更新。如果我向下滾動,新單元格也有拇指但不正確,然後立即更新。這真的很奇怪。

我讀過UICollectionView do not reuse cells,但沒有爲我工作。

//In viewDidLoad 
[_documentCollection registerClass:[DRDDocumentCell class] forCellWithReuseIdentifier:@"Cell"]; 

//DRDDocumentCell interface 
@interface DRDDocumentCell : UICollectionViewCell 
@property (strong, nonatomic) UIImageView *thumbImageView; 
@property (strong, nonatomic) UILabel  *titleLabel; 
@end 

//UICollectionView Delegate 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    DRDDocumentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; 
    [[DRDDocumentManager sharedInstance] configDocumentCell:cell withCellIndex:indexPath.row]; 
    return cell; 
} 

//DRDDocumentManager's configDocumentCell:withCellIndex: method 
- (void)configDocumentCell:(DRDDocumentCell *)cell withCellIndex:(NSInteger)cellIndex { 
    DRDDocument *document = [[DRDDocument alloc] initWithFileURL:_allFileURL[cellIndex]]; 
    [document openWithCompletionHandler:^(BOOL success) { 
     if (!success) NSLog(@"DRDDocumentManager: Opern file error"); 

     [document closeWithCompletionHandler:^(BOOL success) { 
      if (!success) NSLog(@"DRDDocumentManager: Close file error"); 

      cell.titleLabel.text = document.title; 
      cell.thumbImageView.image = document.thumb; 
     }]; 
    }]; 
} 

回答

0

您在跳過單元重用。

加載之前使用過的單元格時,它可能會在圖像視圖中安裝上次使用的圖像。

您的代碼只在下載完成後安裝映像。

您應該更改您的configDocumentCell代碼,先將單元格的文本和圖像設置爲空,然後觸發從URL打開的異步。這樣,單元格將首先顯示空白標籤和圖像,然後在下載完成後,圖像和標籤將顯示。

或者,您可以安裝佔位符文本和插圖,一旦下載完成,插圖就會被替換。

+0

謝謝。有用。但是,細胞似乎還沒有被重用。我的意思是,如果向上滾動隱藏某些單元格然後向下滾動,則所有顯示的單元格都將**重新加載**,而不是立即顯示。我發現[UICollectionView不重用單元格](http://stackoverflow.com/questions/19276509/uicollectionview-do-not-reuse-cells),但沒有奏效。 – wyp