Skip to content

Commit 0c0fc67

Browse files
committed
Comments
1 parent c057169 commit 0c0fc67

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

TestSwift/SearchResultsViewController.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,40 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
4040
var formattedPrice: NSString = rowData["formattedPrice"] as NSString
4141

4242
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
43+
// Jump in to a background thread to get the image for this item
4344

4445
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
4546
var urlString: NSString = rowData["artworkUrl60"] as NSString
46-
//var image: UIImage? = self.imageCache[urlString] as? UIImage
47+
48+
// Check our image cache for the existing key. This is just a dictionary of UIImages
4749
var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage
50+
4851
if( !image? ) {
52+
// If the image does not exist, we need to download it
4953
var imgURL: NSURL = NSURL(string: urlString)
5054

55+
println("Download the image \(imgURL)")
5156
// Download an NSData representation of the image at the URL
5257
var imgData: NSData = NSData(contentsOfURL: imgURL)
5358
image = UIImage(data: imgData)
59+
60+
// Store the image in to our cache
5461
self.imageCache.setValue(image, forKey: urlString)
55-
// self.imageCache[urlString] = image
62+
63+
println("Image downloaded")
5664
}
5765
dispatch_async(dispatch_get_main_queue()) {
66+
// Back on the main thread, let's set the image view of the cell to use our amazing new image
67+
// First, we need to make sure the cell hasn't gone off the screen, or worse, been re-used
68+
// Validate the cell is still available by using cellForRowAtIndexPath
5869
var sCell: UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
59-
if sCell != nil {
70+
71+
// If the cell exists, we're good and we can update it with this image
72+
if sCell? {
6073
let pCell: UITableViewCell = sCell!
6174
pCell.image = image
62-
println("set cell to \(image?.size.width)")
63-
cell.image = image
64-
75+
println("set image")
6576
}
66-
println("attempt to set cell")
6777
}
6878

6979
})

0 commit comments

Comments
 (0)