|
9 | 9 | import UIKit
|
10 | 10 |
|
11 | 11 | class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
|
| 12 | + |
| 13 | + @IBOutlet var appsTableView : UITableView? |
| 14 | + var tableData = [] |
12 | 15 |
|
13 | 16 | override func viewDidLoad() {
|
14 | 17 | 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") |
21 | 19 | }
|
22 | 20 |
|
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 |
25 | 24 | }
|
26 | 25 |
|
27 | 26 | func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
|
28 | 27 | let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
|
29 | 28 |
|
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 |
32 | 45 |
|
33 | 46 | return cell
|
34 | 47 | }
|
| 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 | + } |
35 | 83 |
|
36 | 84 |
|
37 | 85 | }
|
|
0 commit comments