Skip to content

Commit 70ed338

Browse files
committed
Part5
1 parent cda25e1 commit 70ed338

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

SwiftTutorial.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
020BE45719A71EB3006C3E0B /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 020BE45619A71EB3006C3E0B /* Images.xcassets */; };
1414
020BE46319A71EB3006C3E0B /* SwiftTutorialTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE46219A71EB3006C3E0B /* SwiftTutorialTests.swift */; };
1515
020BE46D19A721FF006C3E0B /* APIController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE46C19A721FF006C3E0B /* APIController.swift */; };
16+
020BE46F19A7265F006C3E0B /* Blank52.png in Resources */ = {isa = PBXBuildFile; fileRef = 020BE46E19A7265F006C3E0B /* Blank52.png */; };
1617
/* End PBXBuildFile section */
1718

1819
/* Begin PBXContainerItemProxy section */
@@ -36,6 +37,7 @@
3637
020BE46119A71EB3006C3E0B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3738
020BE46219A71EB3006C3E0B /* SwiftTutorialTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftTutorialTests.swift; sourceTree = "<group>"; };
3839
020BE46C19A721FF006C3E0B /* APIController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIController.swift; sourceTree = "<group>"; };
40+
020BE46E19A7265F006C3E0B /* Blank52.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Blank52.png; sourceTree = "<group>"; };
3941
/* End PBXFileReference section */
4042

4143
/* Begin PBXFrameworksBuildPhase section */
@@ -77,6 +79,7 @@
7779
020BE44C19A71EB3006C3E0B /* SwiftTutorial */ = {
7880
isa = PBXGroup;
7981
children = (
82+
020BE46E19A7265F006C3E0B /* Blank52.png */,
8083
020BE46C19A721FF006C3E0B /* APIController.swift */,
8184
020BE44F19A71EB3006C3E0B /* AppDelegate.swift */,
8285
020BE45119A71EB3006C3E0B /* SearchResultsViewController.swift */,
@@ -192,6 +195,7 @@
192195
isa = PBXResourcesBuildPhase;
193196
buildActionMask = 2147483647;
194197
files = (
198+
020BE46F19A7265F006C3E0B /* Blank52.png in Resources */,
195199
020BE45519A71EB3006C3E0B /* Main.storyboard in Resources */,
196200
020BE45719A71EB3006C3E0B /* Images.xcassets in Resources */,
197201
);

SwiftTutorial/Blank52.png

1.44 KB
Loading

SwiftTutorial/SearchResultsViewController.swift

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
1515
@IBOutlet var appsTableView : UITableView?
1616
var tableData = []
1717
var api = APIController()
18+
var imageCache = [String : UIImage]()
1819

1920
override func viewDidLoad() {
2021
super.viewDidLoad()
@@ -28,23 +29,59 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
2829
}
2930

3031
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
32+
3133
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
3234

33-
let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
35+
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
3436

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")
3641

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)
4442

4543
// Get the formatted price string for display in the subtitle
4644
let formattedPrice: NSString = rowData["formattedPrice"] as NSString
4745

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+
4885
cell.detailTextLabel.text = formattedPrice
4986

5087
return cell

0 commit comments

Comments
 (0)