Skip to content

Instantly share code, notes, and snippets.

@markdillon
Created July 30, 2014 14:29
Show Gist options
  • Save markdillon/4675d6be404423448351 to your computer and use it in GitHub Desktop.
Save markdillon/4675d6be404423448351 to your computer and use it in GitHub Desktop.
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
// Add a check to make sure this exists
let cellText: String? = rowData["trackName"] as? String
cell.textLabel.text = cellText
cell.imageView.image = UIImage(named: "Blank52")
// Get the formatted price string for display in the subtitle
let formattedPrice: NSString = rowData["formattedPrice"] as NSString
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
let urlString: NSString = rowData["artworkUrl60"] as NSString
// Check our image cache for the existing key. This is just a dictionary of UIImages
//var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage
var image = self.imageCache[urlString]
if( !image? ) {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
// Jump in to a background thread to get the image for this item
// If the image does not exist, we need to download it
var imgURL: NSURL = NSURL(string: urlString)
// Download an NSData representation of the image at the URL
let request: NSURLRequest = NSURLRequest(URL: imgURL)
let urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
if !error? {
image = UIImage(data: data)
// Store the image in to our cache
self.imageCache[urlString] = image
cell.imageView.image = image
}
else {
println("Error: \(error.localizedDescription)")
}
})
})
} else {
cell.imageView.image = image
}
cell.detailTextLabel.text = formattedPrice
return cell
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment