Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5: UITableView cells are not scrolling smooth

Using:

  • iOS 5, ARC, StoryBoards.

Have 1 image and text on every cell. Images are not getting resized while scrolling. Have two versions of images image.png and [email protected].

The custom cell was managed by drugging UIImageView and UILabel in storyboard.

Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
Continent *continent=[self.items objectAtIndex:[indexPath row]];
ContinentCell *cell = (ContinentCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.continentName.text=continent.continentName;
    cell.textView.text=continent.countriesHash;
    cell.imageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:continent.continentImage ofType:@"png"]];
 return cell;
}

Where's the evil? Thank you in advance.

like image 587
NCFUSN Avatar asked Nov 17 '25 20:11

NCFUSN


1 Answers

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:continent.continentImage ofType:@"png"]];

Is expensive. You want to load the image the image asynchronously in the background then present it on the main thread when ready.

Here's a very rough solution

    cell.imageView.image = nil;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      UIImage * img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:continent.continentImage ofType:@"png"]];
      dispatch_sync(dispatch_get_main_queue(), ^{
        cell.imageView.image = img;
      });
    });
like image 141
lorean Avatar answered Nov 19 '25 08:11

lorean