Skip to content

Commit 33d58ef

Browse files
committed
Part 2
1 parent e697701 commit 33d58ef

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

SwiftTutorial/Base.lproj/Main.storyboard

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
</subviews>
2828
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
2929
</view>
30+
<connections>
31+
<outlet property="appsTableView" destination="uSB-Wx-Ygo" id="K0F-VR-2Uq"/>
32+
</connections>
3033
</viewController>
3134
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
3235
</objects>

SwiftTutorial/ViewController.swift

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,77 @@
99
import UIKit
1010

1111
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
12+
13+
@IBOutlet var appsTableView : UITableView?
14+
var tableData = []
1215

1316
override func viewDidLoad() {
1417
super.viewDidLoad()
15-
// Do any additional setup after loading the view, typically from a nib.
16-
}
17-
18-
override func didReceiveMemoryWarning() {
19-
super.didReceiveMemoryWarning()
20-
// Dispose of any resources that can be recreated.
18+
searchItunesFor("JQ Software")
2119
}
2220

23-
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
24-
return 10
21+
/// MARK: UITableViewDataSource, UITableViewDelegate methods
22+
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
23+
return tableData.count
2524
}
2625

2726
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
2827
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
2928

30-
cell.textLabel.text = "Row #\(indexPath.row)"
31-
cell.detailTextLabel.text = "Subtitle #\(indexPath.row)"
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
3245

3346
return cell
3447
}
48+
49+
50+
func searchItunesFor(searchTerm: String) {
51+
52+
// The iTunes API wants multiple terms separated by + symbols, so replace spaces with + signs
53+
let itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
54+
55+
// Now escape anything else that isn't URL-friendly
56+
if let escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
57+
let urlPath = "http://itunes.apple.com/search?term=\(escapedSearchTerm)&media=software"
58+
let url: NSURL = NSURL(string: urlPath)
59+
let session = NSURLSession.sharedSession()
60+
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
61+
println("Task completed")
62+
if(error != nil) {
63+
// If there is an error in the web request, print it to the console
64+
println(error.localizedDescription)
65+
}
66+
var err: NSError?
67+
68+
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
69+
if(err != nil) {
70+
// If there is an error parsing JSON, print it to the console
71+
println("JSON Error \(err!.localizedDescription)")
72+
}
73+
let results: NSArray = jsonResult["results"] as NSArray
74+
dispatch_async(dispatch_get_main_queue(), {
75+
self.tableData = results
76+
self.appsTableView!.reloadData()
77+
})
78+
})
79+
80+
task.resume()
81+
}
82+
}
3583

3684

3785
}

0 commit comments

Comments
 (0)