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