Created
June 4, 2014 22:59
-
-
Save jquave/0dc47a403a2f1a1d5440 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { | |
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell | |
if cell == nil { | |
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: kCellIdentifier) | |
} | |
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.text = cellText | |
cell.image = UIImage(named: "Icon76") | |
// Get the formatted price string for display in the subtitle | |
var formattedPrice: NSString = rowData["formattedPrice"] as NSString | |
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { | |
// Jump in to a background thread to get the image for this item | |
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail | |
var 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 | |
if( !image? ) { | |
// 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 | |
var request: NSURLRequest = NSURLRequest(URL: imgURL) | |
var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self) | |
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in | |
if !error? { | |
//var imgData: NSData = NSData(contentsOfURL: imgURL) | |
image = UIImage(data: data) | |
// Store the image in to our cache | |
self.imageCache.setValue(image, forKey: urlString) | |
cell.image = image | |
} | |
else { | |
println("Error: \(error.localizedDescription)") | |
} | |
}) | |
} | |
else { | |
cell.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