Skip to content

Latest commit

 

History

History
57 lines (35 loc) · 2.36 KB

2015_09_26.md

File metadata and controls

57 lines (35 loc) · 2.36 KB

#Objectives

  • What are pros/cons of using .nib vs storyboard for custom UI?
  • What are some problems with loading remote resources on the main thread?
  • What are some strategies to prevent lag when loading remote resources?

Custom UITableViewCell

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/registerNib:forCellReuseIdentifier:

storyboard: storyboard

nib nib

Remote assets in UITableViewCell

The bad way...

Assuming we have a class containing a remote url string property

// ...cellForRowAtIndexPath...

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier" forIndexPath:indexPath];

// access the current object
id object = self.results[indexPath.row];

// create an image from a remote url
NSURL *url = [NSURL URLWithString:object.remoteImageURLString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];

// set the cells image
cell.imageView.image = UIImage image

The problem with the above example is that the line NSData *data = [NSData dataWithContentsOfURL:url]; is actually downloading the asset from the remote server. Until that download task is complete your UI will be completely locked.

The good way...

https://github.com/rs/SDWebImage

It’s very common to load images into a UITableViewCell, for things like user avatars or product thumbnails.

But there are a number of things to watch out for. First, you want to load the image in the background so that the UI doesn’t get blocked as each cell loads.

Also, caching images means you don’t have to re-download the image every time the cell gets loaded. However this can open up its own set of issues, as caching in memory can cause you run out of memory very quickly, and saving to disk requires attention to make sure you don’t use up tons of space needlessly...

http://www.iosdevnotes.com/2011/01/loading-remote-images-into-a-uitableviewcell/

Dependancy management

Cocoapods
https://guides.cocoapods.org/using/getting-started.html