ryujo

Quave's Swift tutorial thumbnail glitch fix

Jun 30th, 2014
438
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
  2.  
  3. var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
  4. if cell == nil {
  5. cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: kCellIdentifier)
  6. }
  7.  
  8. var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
  9.  
  10. // Add a check to make sure this exists
  11. let cellText: String? = rowData["trackName"] as? String
  12. cell.text = cellText
  13.  
  14. var urlString: NSString = rowData["artworkUrl60"] as NSString
  15. var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage
  16. if (!image?) {
  17. cell.image = UIImage(named: "Blank52")
  18. dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
  19.  
  20. // If the image does not exist, we need to download it
  21. var imgURL: NSURL = NSURL(string: urlString)
  22.  
  23. // Download an NSData representation of the image at the URL
  24. var request: NSURLRequest = NSURLRequest(URL: imgURL)
  25. var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)
  26. NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
  27. if !error? {
  28. //var imgData: NSData = NSData(contentsOfURL: imgURL)
  29. image = UIImage(data: data)
  30.  
  31. // Store the image in to our cache
  32. self.imageCache.setValue(image, forKey: urlString)
  33. cell.image = image
  34. }
  35. else {
  36. println("Error: \(error.localizedDescription)")
  37. }
  38. })
  39. })
  40.  
  41. } else {
  42. cell.image = image!
  43. }
  44.  
  45.  
  46.  
  47. // Get the formatted price string for display in the subtitle
  48. var formattedPrice: NSString = rowData["formattedPrice"] as NSString
  49.  
  50. cell.detailTextLabel.text = formattedPrice
  51.  
  52. return cell
  53. }
RAW Paste Data