Skip to content

Commit 049c048

Browse files
committed
Part 5
1 parent d5a1bac commit 049c048

File tree

5 files changed

+175
-9
lines changed

5 files changed

+175
-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
0256BB57194F3F38003E2942 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0256BB56194F3F38003E2942 /* Images.xcassets */; };
1414
0256BB63194F3F38003E2942 /* SwiftTutorialTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0256BB62194F3F38003E2942 /* SwiftTutorialTests.swift */; };
1515
0256BB6D194F41F5003E2942 /* APIController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0256BB6C194F41F5003E2942 /* APIController.swift */; };
16+
0256BB6F194F4852003E2942 /* Blank52.png in Resources */ = {isa = PBXBuildFile; fileRef = 0256BB6E194F4852003E2942 /* Blank52.png */; };
1617
/* End PBXBuildFile section */
1718

1819
/* Begin PBXContainerItemProxy section */
@@ -36,6 +37,7 @@
3637
0256BB61194F3F38003E2942 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3738
0256BB62194F3F38003E2942 /* SwiftTutorialTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftTutorialTests.swift; sourceTree = "<group>"; };
3839
0256BB6C194F41F5003E2942 /* APIController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIController.swift; sourceTree = "<group>"; };
40+
0256BB6E194F4852003E2942 /* 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
0256BB4C194F3F38003E2942 /* SwiftTutorial */ = {
7880
isa = PBXGroup;
7981
children = (
82+
0256BB6E194F4852003E2942 /* Blank52.png */,
8083
0256BB4F194F3F38003E2942 /* AppDelegate.swift */,
8184
0256BB51194F3F38003E2942 /* SearchResultsViewController.swift */,
8285
0256BB53194F3F38003E2942 /* Main.storyboard */,
@@ -192,6 +195,7 @@
192195
isa = PBXResourcesBuildPhase;
193196
buildActionMask = 2147483647;
194197
files = (
198+
0256BB6F194F4852003E2942 /* Blank52.png in Resources */,
195199
0256BB55194F3F38003E2942 /* Main.storyboard in Resources */,
196200
0256BB57194F3F38003E2942 /* Images.xcassets in Resources */,
197201
);
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0600"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "0256BB49194F3F38003E2942"
18+
BuildableName = "SwiftTutorial.app"
19+
BlueprintName = "SwiftTutorial"
20+
ReferencedContainer = "container:SwiftTutorial.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
27+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
28+
shouldUseLaunchSchemeArgsEnv = "YES"
29+
buildConfiguration = "Debug">
30+
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "0256BB5B194F3F38003E2942"
36+
BuildableName = "SwiftTutorialTests.xctest"
37+
BlueprintName = "SwiftTutorialTests"
38+
ReferencedContainer = "container:SwiftTutorial.xcodeproj">
39+
</BuildableReference>
40+
</TestableReference>
41+
</Testables>
42+
<MacroExpansion>
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "0256BB49194F3F38003E2942"
46+
BuildableName = "SwiftTutorial.app"
47+
BlueprintName = "SwiftTutorial"
48+
ReferencedContainer = "container:SwiftTutorial.xcodeproj">
49+
</BuildableReference>
50+
</MacroExpansion>
51+
</TestAction>
52+
<LaunchAction
53+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
54+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
55+
launchStyle = "0"
56+
useCustomWorkingDirectory = "NO"
57+
buildConfiguration = "Debug"
58+
ignoresPersistentStateOnLaunch = "NO"
59+
debugDocumentVersioning = "YES"
60+
allowLocationSimulation = "YES">
61+
<BuildableProductRunnable>
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "0256BB49194F3F38003E2942"
65+
BuildableName = "SwiftTutorial.app"
66+
BlueprintName = "SwiftTutorial"
67+
ReferencedContainer = "container:SwiftTutorial.xcodeproj">
68+
</BuildableReference>
69+
</BuildableProductRunnable>
70+
<AdditionalOptions>
71+
</AdditionalOptions>
72+
</LaunchAction>
73+
<ProfileAction
74+
shouldUseLaunchSchemeArgsEnv = "YES"
75+
savedToolIdentifier = ""
76+
useCustomWorkingDirectory = "NO"
77+
buildConfiguration = "Release"
78+
debugDocumentVersioning = "YES">
79+
<BuildableProductRunnable>
80+
<BuildableReference
81+
BuildableIdentifier = "primary"
82+
BlueprintIdentifier = "0256BB49194F3F38003E2942"
83+
BuildableName = "SwiftTutorial.app"
84+
BlueprintName = "SwiftTutorial"
85+
ReferencedContainer = "container:SwiftTutorial.xcodeproj">
86+
</BuildableReference>
87+
</BuildableProductRunnable>
88+
</ProfileAction>
89+
<AnalyzeAction
90+
buildConfiguration = "Debug">
91+
</AnalyzeAction>
92+
<ArchiveAction
93+
buildConfiguration = "Release"
94+
revealArchiveInOrganizer = "YES">
95+
</ArchiveAction>
96+
</Scheme>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>SwiftTutorial.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>0256BB49194F3F38003E2942</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>0256BB5B194F3F38003E2942</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
</dict>
26+
</dict>
27+
</plist>

SwiftTutorial/Blank52.png

975 Bytes
Loading

SwiftTutorial/SearchResultsViewController.swift

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
1313
@IBOutlet var appsTableView : UITableView
1414
var tableData: NSArray = []
1515
var api: APIController = APIController()
16+
var imageCache = NSMutableDictionary()
17+
let kCellIdentifier = "SearchResultCell"
1618

1719
override func viewDidLoad() {
1820
super.viewDidLoad()
@@ -30,24 +32,61 @@ class SearchResultsViewController: UIViewController, UITableViewDataSource, UITa
3032
}
3133

3234
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
33-
let kCellIdentifier: String = "SearchResultCell"
35+
3436
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell
37+
if cell == nil {
38+
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: kCellIdentifier)
39+
}
3540

3641
var rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
3742

38-
cell.text = rowData["trackName"] as String
39-
40-
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
41-
var urlString: NSString = rowData["artworkUrl60"] as NSString
42-
var imgURL: NSURL = NSURL(string: urlString)
43+
// Add a check to make sure this exists
44+
let cellText: String? = rowData["trackName"] as? String
45+
cell.text = cellText
46+
cell.image = UIImage(named: "Blank52")
4347

44-
// Download an NSData representation of the image at the URL
45-
var imgData: NSData = NSData(contentsOfURL: imgURL)
46-
cell.image = UIImage(data: imgData)
4748

4849
// Get the formatted price string for display in the subtitle
4950
var formattedPrice: NSString = rowData["formattedPrice"] as NSString
5051

52+
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
53+
// Jump in to a background thread to get the image for this item
54+
55+
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
56+
var urlString: NSString = rowData["artworkUrl60"] as NSString
57+
58+
// Check our image cache for the existing key. This is just a dictionary of UIImages
59+
var image: UIImage? = self.imageCache.valueForKey(urlString) as? UIImage
60+
61+
if( !image? ) {
62+
// If the image does not exist, we need to download it
63+
var imgURL: NSURL = NSURL(string: urlString)
64+
65+
// Download an NSData representation of the image at the URL
66+
var request: NSURLRequest = NSURLRequest(URL: imgURL)
67+
var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)
68+
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
69+
if !error? {
70+
//var imgData: NSData = NSData(contentsOfURL: imgURL)
71+
image = UIImage(data: data)
72+
73+
// Store the image in to our cache
74+
self.imageCache.setValue(image, forKey: urlString)
75+
cell.image = image
76+
}
77+
else {
78+
println("Error: \(error.localizedDescription)")
79+
}
80+
})
81+
82+
}
83+
else {
84+
cell.image = image
85+
}
86+
87+
88+
})
89+
5190
cell.detailTextLabel.text = formattedPrice
5291

5392
return cell

0 commit comments

Comments
 (0)