Skip to content

Commit 363bed8

Browse files
committed
Part 5
1 parent 26ca271 commit 363bed8

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

MusicPlayer.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
0270481319C8E08B00FDA1C5 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0270481119C8E08B00FDA1C5 /* LaunchScreen.xib */; };
1515
0270481F19C8E08B00FDA1C5 /* MusicPlayerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270481E19C8E08B00FDA1C5 /* MusicPlayerTests.swift */; };
1616
0270482919C8E2E700FDA1C5 /* APIController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0270482819C8E2E700FDA1C5 /* APIController.swift */; };
17+
0270482B19C8EC3000FDA1C5 /* Blank52.png in Resources */ = {isa = PBXBuildFile; fileRef = 0270482A19C8EC3000FDA1C5 /* Blank52.png */; };
1718
/* End PBXBuildFile section */
1819

1920
/* Begin PBXContainerItemProxy section */
@@ -38,6 +39,7 @@
3839
0270481D19C8E08B00FDA1C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3940
0270481E19C8E08B00FDA1C5 /* MusicPlayerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicPlayerTests.swift; sourceTree = "<group>"; };
4041
0270482819C8E2E700FDA1C5 /* APIController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIController.swift; sourceTree = "<group>"; };
42+
0270482A19C8EC3000FDA1C5 /* Blank52.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = Blank52.png; sourceTree = "<group>"; };
4143
/* End PBXFileReference section */
4244

4345
/* Begin PBXFrameworksBuildPhase section */
@@ -79,6 +81,7 @@
7981
0270480519C8E08B00FDA1C5 /* MusicPlayer */ = {
8082
isa = PBXGroup;
8183
children = (
84+
0270482A19C8EC3000FDA1C5 /* Blank52.png */,
8285
0270480819C8E08B00FDA1C5 /* AppDelegate.swift */,
8386
0270480A19C8E08B00FDA1C5 /* SearchResultsViewController.swift */,
8487
0270480C19C8E08B00FDA1C5 /* Main.storyboard */,
@@ -196,6 +199,7 @@
196199
buildActionMask = 2147483647;
197200
files = (
198201
0270480E19C8E08B00FDA1C5 /* Main.storyboard in Resources */,
202+
0270482B19C8EC3000FDA1C5 /* Blank52.png in Resources */,
199203
0270481319C8E08B00FDA1C5 /* LaunchScreen.xib in Resources */,
200204
0270481019C8E08B00FDA1C5 /* Images.xcassets in Resources */,
201205
);
@@ -409,6 +413,7 @@
409413
0270482419C8E08B00FDA1C5 /* Release */,
410414
);
411415
defaultConfigurationIsVisible = 0;
416+
defaultConfigurationName = Release;
412417
};
413418
0270482519C8E08B00FDA1C5 /* Build configuration list for PBXNativeTarget "MusicPlayerTests" */ = {
414419
isa = XCConfigurationList;
@@ -417,6 +422,7 @@
417422
0270482719C8E08B00FDA1C5 /* Release */,
418423
);
419424
defaultConfigurationIsVisible = 0;
425+
defaultConfigurationName = Release;
420426
};
421427
/* End XCConfigurationList section */
422428
};

MusicPlayer/Blank52.png

1.44 KB
Loading

MusicPlayer/SearchResultsViewController.swift

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
1313
@IBOutlet var appsTableView : UITableView?
1414
var tableData = []
1515
var api = APIController()
16+
var imageCache = [String : UIImage]()
1617
let kCellIdentifier: String = "SearchResultCell"
1718

1819
override func viewDidLoad() {
@@ -31,23 +32,62 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
3132
}
3233

3334
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
35+
3436
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
3537

36-
let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
38+
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
3739

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

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

4846
// Get the formatted price string for display in the subtitle
4947
let formattedPrice: NSString = rowData["formattedPrice"] as NSString
5048

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+
5191
cell.detailTextLabel?.text = formattedPrice
5292

5393
return cell

0 commit comments

Comments
 (0)