PDA

View Full Version : TableView scroll is choppy




jackhdev
Mar 4, 2012, 04:11 PM
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.

// 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;

}



pmau
Mar 4, 2012, 04:22 PM
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.

jackhdev
Mar 4, 2012, 04:33 PM
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.

Do you know the name of it?

pmau
Mar 4, 2012, 05:04 PM
LazyTableImages (https://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009394)

I hope the link works OK.

----------

There was also some advanced sample code from WWDC 2010.
I'll try to find it...

PhoneyDeveloper
Mar 4, 2012, 07:39 PM
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.