I have a CollectionView which displays some info about users. One of these items is a user avatar (100x100 png), which is loaded from our webserver. The problems is that it seems to me the view reloads the cells when they have gone outside the screen region. Every time I scroll the page it lags for a bit when the cells that were entirely outside the screen region enter the screen. It's not a really big problem on wifi, but on 3g this is really slow, plus it consumes bandwidth. Overall, it's just bad to handle data like this for my app. I want to load the data once and display it, and not have hundreds of webrequests when the user scrolls the page. Is there any way to load the cells once, and leave them like that until I update them manually? I don't do any updating yet, this is my current code:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return numContacts;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UILabel *contactName = (UILabel *)[cell viewWithTag:1];
char *text = contactArray[indexPath.row].name;
contactName.text = [NSString stringWithCString:text encoding:NSASCIIStringEncoding];
NSData * presence = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline" ofType:@"png"]];
UIImageView *presenceView = (UIImageView *)[cell viewWithTag:2];
presenceView.image = [UIImage imageWithData: presence];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"myWebServer",text]];
NSData * avatar = [[NSData alloc] initWithContentsOfURL:url];
UIImageView *avatarView = (UIImageView *)[cell viewWithTag:3];
avatarView.image = [UIImage imageWithData: avatar];
NSData *placeholder = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"avatar_unknown" ofType:@"png"]];
[avatarView setImageWithURLRequest:[NSURLRequest requestWithURL:url] placeholderImage:[UIImage imageWithData:placeholder] success:nil failure:nil];
return cell;
}