9
9
import UIKit
10
10
11
11
class ViewController : UIViewController {
12
-
12
+
13
+ @IBOutlet var appsTableView : UITableView ?
14
+ var tableData = [ ]
15
+
13
16
override func viewDidLoad( ) {
14
17
super. viewDidLoad ( )
15
- // Do any additional setup after loading the view, typically from a nib.
18
+ searchItunesFor ( " JQ Software " )
16
19
}
17
20
18
21
override func didReceiveMemoryWarning( ) {
@@ -21,17 +24,67 @@ class ViewController: UIViewController {
21
24
}
22
25
23
26
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
26
29
}
30
+
27
31
func tableView( tableView: UITableView ! , cellForRowAtIndexPath indexPath: NSIndexPath ! ) -> UITableViewCell ! {
28
32
let cell : UITableViewCell = UITableViewCell ( style: UITableViewCellStyle . Subtitle, reuseIdentifier: " MyTestCell " )
29
33
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
32
50
33
51
return cell
34
52
}
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
+ }
35
88
36
89
37
90
}
0 commit comments