There are several changes that need to be made, first you should add a UIImageView when the cell is generated, and not every time when the tableView:cellForRowAtIndexPath: hits (as @Vishy suggests). Secondly, you must cache the images that you download from the document directory ( [UIImage imageNamed:] does this automatically for package resources).
@interface MyViewController () { NSMutableDictionary *_imageCache; } @end @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; // other viewDidLoad stuff... _imageCache = [[NSMutableDictionary alloc] init]; } - (void)viewDidUnload { [super viewDidUnload]; // other viewDidUnload stuff... [_imageCache release]; _imageCache = nil; } - (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; UIImageView *whiteView = [[UIImageView alloc] initWithFrame:CGRectMake((projectsTable.frame.size.width/2)-150, 4, 204.8, 153.6)]; whiteView.tag = 111; whiteView.backgroundColor = [UIColor whiteColor]; [cell.contentView addSubview:whiteView]; [whiteView release]; cell.accessoryType = UITableViewCellAccessoryNone; cell.textLabel.hidden = YES; } UIImageView* iView = (UIImageView*) [cell.contentView viewWithTag:111]; if([currSection isEqualToString:@"composer"]) { MySlide *s = [slidesArray objectAtIndex:indexPath.row]; if([s.slideImage isEqualToString:@""] || s.slideImage == nil) { //no custom image in this cell - go with default background image iView.image = [UIImage imageNamed:@"cellback2.png"]; } else { cell.layer.shouldRasterize = YES; cell.layer.rasterizationScale = [UIScreen mainScreen].scale; // use the image path as the cache key UIImage *theImage = [_imageCache objectForKey:s.slideImage]; if (theImage == nil) { // load the image and save into the cache theImage = [UIImage imageWithContentsOfFile:s.slideImage]; theImage = [self imageWithImage:theImage CovertToSize:CGSizeMake(204.8, 153.6)]; [_imageCache setObject:theImage forKey:s.slideImage]; } iView.image = theImage; } } } @end
As a rule, tableView:cellForRowAtIndexPath: is a method that you need to exit quickly , so avoid loading images from disk whenever possible.
source share