I have issue with redrawing images of uicollectionview cells - images are downloaded from URL parsed from JSON. I have 10 cells and with the start of app only first 6 are displayed, but images are not loaded, when I scroll down to those other cells, they have their images, and when I scroll back to the top, first 4 cells have their images as well, only cells 5 and 6 are not redrawn(they were visible for the whole time). I've been trying to debug this for long time but no success.
in cellForItemAtIndexPath I'm calling SDWebImage(but it doesn't really matter):
[cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
but when cellForItemAtIndexPath is called for the first time, timeSnap.thumbnailImageURL(where timeSnap entity is model for cell) is not initialised yet and so it's null
after initialisation of timeSnap.thumbnailImageURL I have:
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
edit1: adding my code, it's a bit complex - first I'll get JSON from server API with items to be displayed in collection view, then for each item I have to get another JSON from server API, from which I can build URL for timeSnap.thumbnailImageURL
viewController class:
- (void)viewDidLoad
{
[super viewDidLoad];
[self refreshNewTimeSnapsFeed];
}
- (void)refreshNewTimeSnapsFeed {
Adapter *adapter = [AppDelegate instance].adapter;
[adapter getNewTimeSnapsWithCompletion:^(NSArray* jsonResponse) {
[self newTimeSnapsFeedRefreshed:jsonResponse];
}];
}
- (void)newTimeSnapsFeedRefreshed:(NSArray*)jsonResponse {
Adapter *adapter = [AppDelegate instance].adapter;
for (NSDictionary *timeSnapDict in jsonResponse) {
TimeSnap *timeSnap = [[TimeSnap alloc] initWithJSON:timeSnapDict];
[adapter getTimeSnapSnapsforId:timeSnap.id withCompletion:^(NSArray* jsonResponse) {
[timeSnap getSnapsJSON:jsonResponse];
[timeSnap getTimeSnapThumbnailImageURL];
}];
[self.timeSnaps addObject:timeSnap];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
});
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
TimeSnapCell *cell = (TimeSnapCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"TimeSnapCell" forIndexPath:indexPath];
TimeSnap *timeSnap = self.timeSnaps[indexPath.row];
cell.label.text = timeSnap.name;
[cell.backgroundImage setImageWithURL:[NSURL URLWithString:timeSnap.thumbnailImageURL] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
return cell;
}
adapter class:
- (void)getNewTimeSnapsWithCompletion:(void (^)(NSArray* jsonResponse))completion {
NSString *urlAsString = [NSString stringWithFormat:@"..."];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
[self getJSONWithCompletion:completion fromURL:url forKey:@"Items"];
}
- (void)getTimeSnapSnapsforId:(NSNumber*)id withCompletion:(void (^)(NSArray* jsonResponse))completion {
NSString *urlAsString = [NSString stringWithFormat:@"..."];
NSURL *url = [[NSURL alloc] initWithString:urlAsString];
[self getJSONWithCompletion:completion fromURL:url forKey:@"Items"];
}
- (void)getJSONWithCompletion:(void (^)(NSArray* jsonResponse))completion fromURL:(NSURL*)url forKey:(NSString*)key {
//code for getting JSON from server API, then parsing into NSArray *items
dispatch_async(dispatch_get_main_queue(), ^{
if(completion){
completion(items);
}
});
}