Skip to content

Commit 41763f4

Browse files
committed
Part 2
1 parent 382fb53 commit 41763f4

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

MusicPlayer/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="ODz-Kv-YZf" id="GMq-6f-r3P"/>
32+
</connections>
3033
</viewController>
3134
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
3235
</objects>

MusicPlayer/ViewController.swift

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
import UIKit
1010

1111
class ViewController: UIViewController {
12-
12+
13+
@IBOutlet var appsTableView : UITableView?
14+
var tableData = []
15+
1316
override func viewDidLoad() {
1417
super.viewDidLoad()
15-
// Do any additional setup after loading the view, typically from a nib.
18+
searchItunesFor("JQ Software")
1619
}
1720

1821
override func didReceiveMemoryWarning() {
@@ -21,17 +24,67 @@ class ViewController: UIViewController {
2124
}
2225

2326

24-
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
25-
return 10
27+
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
28+
return tableData.count
2629
}
30+
2731
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
2832
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
2933

30-
cell.textLabel?.text = "Row #\(indexPath.row)"
31-
cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"
34+
let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
35+
36+
cell.textLabel?.text = rowData["trackName"] as? String
37+
38+
// Grab the artworkUrl60 key to get an image URL for the app's thumbnail
39+
let urlString: NSString = rowData["artworkUrl60"] as NSString
40+
let imgURL: NSURL = NSURL(string: urlString)
41+
42+
// Download an NSData representation of the image at the URL
43+
let imgData: NSData = NSData(contentsOfURL: imgURL)
44+
cell.imageView?.image = UIImage(data: imgData)
45+
46+
// Get the formatted price string for display in the subtitle
47+
let formattedPrice: NSString = rowData["formattedPrice"] as NSString
48+
49+
cell.detailTextLabel?.text = formattedPrice
3250

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

3689

3790
}

0 commit comments

Comments
 (0)