Reputation: 6940
I have custom class that load jSON data from server, and when that data is loaded, i load image for each cell using AFNetworking. However, there is annoying effect of sharping, not smoothy scrolling, when i scroll down to new images to be load. There is my method:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Create cell
static NSString *cellIdentifier = @"cell";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCellView" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Check condition, either we got objects
if (!isDataLoaded){
cell.myActivityIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
cell.myActivityIndicator.center=self.view.center;
[cell.myActivityIndicator startAnimating];
[self.view addSubview:cell.myActivityIndicator];
NSLog(@"Current loading");
} else {
// Hide acitivity indicator
// Set image
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
//
// data = [[NSData alloc] initWithContentsOfURL:[[self.objectsToShow objectAtIndex:indexPath.row]valueForKey:@"low_resolutionImage"]];
// UIImage *image = [[UIImage alloc] initWithData:data];
//
// dispatch_async(dispatch_get_main_queue(), ^{
//
// cell.myActivityIndicator.hidden = YES;
// [cell.myActivityIndicator removeFromSuperview];
//
// [cell.myImageView setImage:image];
// });
//
// });
[cell.myImageView setImageWithURL:[[self.objectsToShow objectAtIndex:indexPath.row]valueForKey:@"low_resolutionImage"]];
}
return cell;
}
Upvotes: 0
Views: 64
Reputation: 3321
You don't want to download(async download is MUST) the image every time when the table cell is about to show. Instead, you save the image in a data model after it's downloaded for the first time. Afterward, you can simply retrieve the image from the model. I normally use NSMutableArray to implement the model because it's easier to add/remove its elements.
Upvotes: 1