Reputation: 67
I have a UICollectionview
which is loaded on the basis of the results of a serchbar
in a previous UIViewController
. In the cells of the collection I load images from the internet. When collectionView:(UICollectionView *)cv cellForItemAtIndexPath:
is called I place a placeholder on the image of the cell, and start downloading the image from the internet.
When downloading is done, i reload the cell so the final image is placed properly. So the method is called twice for every cell.
This is the part of the code which may be of interest:
- (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 %ld@",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 {
}
}];
}
}
The odd thing comes now. When I first load the collectionview for a specific search in the previous search bar the behaviour is the expected one. Every cell is called twice, and i can see for a short time the placeholder in the cells and then the different images appear. So everything is OK.
But, if i go back to the search bar -popViewController
from the one the collectionview is displayed on- something strange happens if i load the same collection view again -the same result of the search bar selected-. First I can see the old cells - I know they are the old ones because the method for new ones is not called- then I see the placeholder and eventually the final image which happens to be the same than in the older cells, but the effects not good, because you see: image-placeholder-image.
The thing is that method to define cells is only called twice for every cell, as it should, but the initial content of the cell is shown.
I have read some similar questions and i have tried two approaches without success:
- I have implemented the Prepareforreuse
method in my cell class.
- I have used the -(void)collectionView:(UICollectionView *)cv didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
method.
As I have said no result. Any other idea? I am with this issue for two days now, and i am really quite desperate.
Upvotes: 2
Views: 717