Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jackhdev

macrumors 6502
Original poster
Apr 9, 2011
343
0
Bismarck, North Dakota
I put UIImageViews into my table view cells and scrolling has been very choppy. I'm assuming it's because the images are being reloaded each time their cell appears, but how would I preload/cache the images? Thanks for your help.

Code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    
    // Set up the cell...
	NSDictionary *dic = [self.issues objectAtIndex:indexPath.row];
        
    cell.text = [dic objectForKey:@"Date"];
    
    cell.imageView.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/issues/%@/cover.png", documentsDirectory, [dic objectForKey:@"Directory Name"]]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        return 150;
    
}
 
Excuse my answer, but there's a complete iOS sample application on apple's developer site that explain exactly what you are trying to do.

Hint: It uses GCD to load images in the background, displaying a placeholder image.
 
LazyTableImages shows how to download images from the internet to display in your table. Just loading them from disk shouldn't be such a problem. Just cache the images in your data model. You can load them in cellForRowAtIndexPath if they haven't already been loaded, if you like. Or if there's a better place to pre-load them you might do that in viewDidLoad. Depends how many rows there might be. If the same images might be used in multiple rows then it might make sense for the view controller to cache them also.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.