我有一個uicollectionview,它是根據以前的uiviewcontroller中serchbar的結果加載的。在收集的單元格中,我加載了來自互聯網的圖像。當collectionView:(UICollectionView *)cv cellForItemAtIndexPath:被調用時,我在單元格的圖像上放置一個佔位符,並開始從互聯網下載圖像。 下載完成後,我重新加載單元格,以便正確放置最終圖像。所以每個單元格調用該方法兩次。UICollectionView顯示舊單元格在重用時的值
這是代碼可能會感興趣的部分: - (TXCeldaPOICollection *)的CollectionView:(UICollectionView *)CV cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TXCeldaPOICollection *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CeldaPOI" forIndexPath:indexPath];
...
NSNumber *estaDisponible=[disponibles objectAtIndex:indexPath.row];
if ([estaDisponible boolValue]==YES) {
cell.imgCelda.image=[imagenes objectAtIndex:indexPath.row];
NSLog(@"Imagen disponible en la celda numero numero %ld",(long)indexPath.row);
} else {
cell.imgCelda.image=[UIImage imageNamed:@"placeholder.png"];
NSLog(@"Placeholder colocado en la celda numero numero %ld",(long)indexPath.row);
// Monto la URL de descarga:
NSString *urlDescarga=kBaseURL;
urlDescarga=[urlDescarga stringByAppendingString:[[self.listaCeldas objectAtIndex:indexPath.row] valueForKey:@"foto1"]];
NSURL *direccion=[NSURL URLWithString:urlDescarga];
[self downloadImageWithURL:direccion conOrden:self.estadioFiltrado completionBlock:^(BOOL succeeded, UIImage *image, NSInteger ordenacion) {
if (succeeded) {
NSLog(@"Imagen descargada estadio %ld orden %ld indice %ld poi %[email protected]",self.estadioFiltrado, ordenacion, indexPath.row, [seleccionado.identificador integerValue]);
if (ordenacion==self.estadioFiltrado) {
if (image!=nil) {
//Meto comprobacion de no rebase del array aunque genere algun error me protejo frente a cueelgues
NSInteger numeroImagenes= imagenes.count;
if (indexPath.row<=numeroImagenes-1) {
[imagenes replaceObjectAtIndex:indexPath.row withObject:image];
[disponibles replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];
NSArray *recargar=[[NSArray alloc] initWithObjects:indexPath, nil];
NSLog(@"Imagen DESCARGADA numero %ld y llamando a recargar ccelda",(long)indexPath.row);
[self.coleccion reloadItemsAtIndexPaths:recargar];
}
}
}
} else {
}
}];
}
}
奇怪的事情來,現在。當我第一次在上一個搜索欄中加載特定搜索的collectionview時,行爲就是預期的行爲。每個單元格被調用兩次,我可以在短時間內看到單元格中的佔位符,然後出現不同的圖像。所以一切都好。
但是,如果我從顯示collectionview的那個回到搜索欄-popViewController - 如果我再次加載相同的集合視圖 - 選擇的搜索欄的結果相同,會發生一些奇怪的事情。首先,我可以看到舊的細胞 - 我知道它們是舊的細胞,因爲沒有調用新細胞的方法 - 然後我看到佔位符,最終看到的是最終的圖像,這與舊的細胞相同,但效果是不好,因爲你看到:image-placeholder-image。
這個東西是定義單元格的方法,每個單元格只調用兩次,因爲它應該這樣,但是單元格的初始內容會顯示出來。
我已經閱讀了一些類似的問題,我已經嘗試了兩種方法,但沒有成功: - 我在我的單元類中實現了Prepareforreuse方法。我使用了 - (void)collectionView:(UICollectionView *)cv didEndDisplayingCell :(UICollectionViewCell *)單元forItemAtIndexPath:(NSIndexPath *)indexPath方法。
正如我所說沒有結果。任何其他想法?我現在在這個問題上已經兩天了,而且我真的非常絕望。
您可以爲搜索選擇添加代碼,您可能需要在從搜索欄中選擇後顯式重新加載收藏視圖 – YuviGr 2015-02-10 00:20:11
搜索欄位於以前的uiviewcontroller中,搜索結果顯示在表格中,當選擇一個其中我推一個新的控制器,我顯示集合,所以實際上我總是重新加載搜索後的集合,因爲我正在加載一個新的uiviewcontroller – user3188554 2015-02-10 08:49:55
你有沒有試過放置一個斷點來看看實際上是否已經調用了出列方法當你回到搜索結果? – YuviGr 2015-02-10 12:39:52