Skip to content

Commit 83f7275

Browse files
committed
Part3
1 parent 33d58ef commit 83f7275

File tree

4 files changed

+84
-51
lines changed

4 files changed

+84
-51
lines changed

SwiftTutorial.xcodeproj/project.pbxproj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
/* Begin PBXBuildFile section */
1010
020BE45019A71EB3006C3E0B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE44F19A71EB3006C3E0B /* AppDelegate.swift */; };
11-
020BE45219A71EB3006C3E0B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE45119A71EB3006C3E0B /* ViewController.swift */; };
11+
020BE45219A71EB3006C3E0B /* SearchResultsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE45119A71EB3006C3E0B /* SearchResultsViewController.swift */; };
1212
020BE45519A71EB3006C3E0B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 020BE45319A71EB3006C3E0B /* Main.storyboard */; };
1313
020BE45719A71EB3006C3E0B /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 020BE45619A71EB3006C3E0B /* Images.xcassets */; };
1414
020BE46319A71EB3006C3E0B /* SwiftTutorialTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE46219A71EB3006C3E0B /* SwiftTutorialTests.swift */; };
15+
020BE46D19A721FF006C3E0B /* APIController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE46C19A721FF006C3E0B /* APIController.swift */; };
1516
/* End PBXBuildFile section */
1617

1718
/* Begin PBXContainerItemProxy section */
@@ -28,12 +29,13 @@
2829
020BE44A19A71EB3006C3E0B /* SwiftTutorial.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftTutorial.app; sourceTree = BUILT_PRODUCTS_DIR; };
2930
020BE44E19A71EB3006C3E0B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3031
020BE44F19A71EB3006C3E0B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
31-
020BE45119A71EB3006C3E0B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
32+
020BE45119A71EB3006C3E0B /* SearchResultsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchResultsViewController.swift; sourceTree = "<group>"; };
3233
020BE45419A71EB3006C3E0B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
3334
020BE45619A71EB3006C3E0B /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
3435
020BE45C19A71EB3006C3E0B /* SwiftTutorialTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftTutorialTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3536
020BE46119A71EB3006C3E0B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3637
020BE46219A71EB3006C3E0B /* SwiftTutorialTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftTutorialTests.swift; sourceTree = "<group>"; };
38+
020BE46C19A721FF006C3E0B /* APIController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIController.swift; sourceTree = "<group>"; };
3739
/* End PBXFileReference section */
3840

3941
/* Begin PBXFrameworksBuildPhase section */
@@ -75,8 +77,9 @@
7577
020BE44C19A71EB3006C3E0B /* SwiftTutorial */ = {
7678
isa = PBXGroup;
7779
children = (
80+
020BE46C19A721FF006C3E0B /* APIController.swift */,
7881
020BE44F19A71EB3006C3E0B /* AppDelegate.swift */,
79-
020BE45119A71EB3006C3E0B /* ViewController.swift */,
82+
020BE45119A71EB3006C3E0B /* SearchResultsViewController.swift */,
8083
020BE45319A71EB3006C3E0B /* Main.storyboard */,
8184
020BE45619A71EB3006C3E0B /* Images.xcassets */,
8285
020BE44D19A71EB3006C3E0B /* Supporting Files */,
@@ -208,8 +211,9 @@
208211
isa = PBXSourcesBuildPhase;
209212
buildActionMask = 2147483647;
210213
files = (
211-
020BE45219A71EB3006C3E0B /* ViewController.swift in Sources */,
214+
020BE45219A71EB3006C3E0B /* SearchResultsViewController.swift in Sources */,
212215
020BE45019A71EB3006C3E0B /* AppDelegate.swift in Sources */,
216+
020BE46D19A721FF006C3E0B /* APIController.swift in Sources */,
213217
);
214218
runOnlyForDeploymentPostprocessing = 0;
215219
};
Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,24 @@
11
//
2-
// ViewController.swift
2+
// APIController.swift
33
// SwiftTutorial
44
//
55
// Created by Jameson Quave on 8/22/14.
66
// Copyright (c) 2014 JQ Software LLC. All rights reserved.
77
//
88

9-
import UIKit
9+
import Foundation
1010

11-
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
11+
protocol APIControllerProtocol {
12+
func didReceiveAPIResults(results: NSDictionary)
13+
}
14+
15+
class APIController {
1216

13-
@IBOutlet var appsTableView : UITableView?
14-
var tableData = []
15-
16-
override func viewDidLoad() {
17-
super.viewDidLoad()
18-
searchItunesFor("JQ Software")
19-
}
17+
var delegate: APIControllerProtocol?
2018

21-
/// MARK: UITableViewDataSource, UITableViewDelegate methods
22-
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
23-
return tableData.count
19+
init() {
2420
}
2521

26-
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
27-
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
28-
29-
let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
30-
31-
cell.textLabel.text = rowData["trackName"] as String
32-
33-
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
34-
let urlString: NSString = rowData["artworkUrl60"] as NSString
35-
let imgURL: NSURL = NSURL(string: urlString)
36-
37-
// Download an NSData representation of the image at the URL
38-
let imgData: NSData = NSData(contentsOfURL: imgURL)
39-
cell.imageView.image = UIImage(data: imgData)
40-
41-
// Get the formatted price string for display in the subtitle
42-
let formattedPrice: NSString = rowData["formattedPrice"] as NSString
43-
44-
cell.detailTextLabel.text = formattedPrice
45-
46-
return cell
47-
}
48-
49-
5022
func searchItunesFor(searchTerm: String) {
5123

5224
// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
@@ -71,16 +43,12 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
7143
println("JSON Error \(err!.localizedDescription)")
7244
}
7345
let results: NSArray = jsonResult["results"] as NSArray
74-
dispatch_async(dispatch_get_main_queue(), {
75-
self.tableData = results
76-
self.appsTableView!.reloadData()
77-
})
46+
self.delegate?.didReceiveAPIResults(jsonResult)
47+
7848
})
7949

8050
task.resume()
8151
}
8252
}
83-
84-
85-
}
86-
53+
54+
}

SwiftTutorial/Base.lproj/Main.storyboard

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7026.1"/>
55
</dependencies>
66
<scenes>
7-
<!--View Controller-->
7+
<!--Search Results View Controller-->
88
<scene sceneID="ufC-wZ-h7g">
99
<objects>
10-
<viewController id="vXZ-lx-hvc" customClass="ViewController" customModule="SwiftTutorial" customModuleProvider="target" sceneMemberID="viewController">
10+
<viewController id="vXZ-lx-hvc" customClass="SearchResultsViewController" customModule="SwiftTutorial" customModuleProvider="target" sceneMemberID="viewController">
1111
<layoutGuides>
1212
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
1313
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// ViewController.swift
3+
// SwiftTutorial
4+
//
5+
// Created by Jameson Quave on 8/22/14.
6+
// Copyright (c) 2014 JQ Software LLC. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class SearchResultsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, APIControllerProtocol {
12+
13+
@IBOutlet var appsTableView : UITableView?
14+
var tableData = []
15+
var api = APIController()
16+
17+
override func viewDidLoad() {
18+
super.viewDidLoad()
19+
api.searchItunesFor("JQ Software")
20+
api.delegate = self
21+
}
22+
23+
/// MARK: UITableViewDataSource, UITableViewDelegate methods
24+
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
25+
return tableData.count
26+
}
27+
28+
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
29+
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
30+
31+
let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
32+
33+
cell.textLabel.text = rowData["trackName"] as String
34+
35+
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
36+
let urlString: NSString = rowData["artworkUrl60"] as NSString
37+
let imgURL: NSURL = NSURL(string: urlString)
38+
39+
// Download an NSData representation of the image at the URL
40+
let imgData: NSData = NSData(contentsOfURL: imgURL)
41+
cell.imageView.image = UIImage(data: imgData)
42+
43+
// Get the formatted price string for display in the subtitle
44+
let formattedPrice: NSString = rowData["formattedPrice"] as NSString
45+
46+
cell.detailTextLabel.text = formattedPrice
47+
48+
return cell
49+
}
50+
51+
func didReceiveAPIResults(results: NSDictionary) {
52+
var resultsArr: NSArray = results["results"] as NSArray
53+
dispatch_async(dispatch_get_main_queue(), {
54+
self.tableData = resultsArr
55+
self.appsTableView!.reloadData()
56+
})
57+
}
58+
59+
60+
}
61+

0 commit comments

Comments
 (0)