@@ -15,6 +15,7 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
15
15
@IBOutlet var appsTableView : UITableView ?
16
16
var tableData = [ ]
17
17
var api = APIController ( )
18
+ var imageCache = [ String : UIImage] ( )
18
19
19
20
override func viewDidLoad( ) {
20
21
super. viewDidLoad ( )
@@ -28,23 +29,59 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
28
29
}
29
30
30
31
func tableView( tableView: UITableView ! , cellForRowAtIndexPath indexPath: NSIndexPath ! ) -> UITableViewCell ! {
32
+
31
33
let cell : UITableViewCell = tableView. dequeueReusableCellWithIdentifier ( kCellIdentifier) as UITableViewCell
32
34
33
- let rowData : NSDictionary = self . tableData [ indexPath. row] as NSDictionary
35
+ var rowData : NSDictionary = self . tableData [ indexPath. row] as NSDictionary
34
36
35
- cell. textLabel. text = rowData [ " trackName " ] as String
37
+ // Add a check to make sure this exists
38
+ let cellText : String ? = rowData [ " trackName " ] as? String
39
+ cell. textLabel. text = cellText
40
+ cell. imageView. image = UIImage ( named: " Blank52 " )
36
41
37
- // Grab the artworkUrl60 key to get an image URL for the app's thumbnail
38
- let urlString : NSString = rowData [ " artworkUrl60 " ] as NSString
39
- let imgURL : NSURL = NSURL ( string: urlString)
40
-
41
- // Download an NSData representation of the image at the URL
42
- let imgData : NSData = NSData ( contentsOfURL: imgURL)
43
- cell. imageView. image = UIImage ( data: imgData)
44
42
45
43
// Get the formatted price string for display in the subtitle
46
44
let formattedPrice : NSString = rowData [ " formattedPrice " ] as NSString
47
45
46
+ // Jump in to a background thread to get the image for this item
47
+
48
+ // Grab the artworkUrl60 key to get an image URL for the app's thumbnail
49
+ let urlString = rowData [ " artworkUrl60 " ] as String
50
+
51
+ // Check our image cache for the existing key. This is just a dictionary of UIImages
52
+ var image = self . imageCache [ urlString]
53
+
54
+
55
+ if ( image == nil ) {
56
+ // If the image does not exist, we need to download it
57
+ var imgURL : NSURL = NSURL ( string: urlString)
58
+
59
+ // Download an NSData representation of the image at the URL
60
+ let request : NSURLRequest = NSURLRequest ( URL: imgURL)
61
+ NSURLConnection . sendAsynchronousRequest ( request, queue: NSOperationQueue . mainQueue ( ) , completionHandler: { ( response: NSURLResponse!, data: NSData!, error: NSError!) - > Void in
62
+ if error == nil {
63
+ image = UIImage ( data: data)
64
+
65
+ // Store the image in to our cache
66
+ self . imageCache [ urlString] = image
67
+ if let cellToUpdate = tableView. cellForRowAtIndexPath ( indexPath) {
68
+ cellToUpdate. imageView. image = image
69
+ }
70
+ }
71
+ else {
72
+ println ( " Error: \( error. localizedDescription) " )
73
+ }
74
+ } )
75
+
76
+ }
77
+ else {
78
+ dispatch_async ( dispatch_get_main_queue ( ) , {
79
+ if let cellToUpdate = tableView. cellForRowAtIndexPath ( indexPath) {
80
+ cellToUpdate. imageView. image = image
81
+ }
82
+ } )
83
+ }
84
+
48
85
cell. detailTextLabel. text = formattedPrice
49
86
50
87
return cell
0 commit comments