-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoursesViewController.swift
More file actions
104 lines (84 loc) · 3.43 KB
/
CoursesViewController.swift
File metadata and controls
104 lines (84 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// CoursesViewController.swift
// Nerdfeed
//
// Created by Troy Abel on 10/23/14.
// Copyright (c) 2014 Troy Abel. All rights reserved.
//
import UIKit
class CoursesViewController: UITableViewController {
var session: NSURLSession!
var courses = [[NSObject:AnyObject]]()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
session = NSURLSession(configuration: config, delegate: nil, delegateQueue: nil)
fetchFeed()
}
func fetchFeed() {
let requestString = "http://bookapi.bignerdranch.com/courses.json"
if let url = NSURL(string: requestString) {
let req = NSURLRequest(URL: url)
let dataTask = session.dataTaskWithRequest(req) {
(data, response, error) in
if data != nil {
var error: NSError?
if let jsonObject = NSJSONSerialization.JSONObjectWithData(data,
options: nil,
error: &error) as? [NSObject:AnyObject] {
if let courseArray: AnyObject = jsonObject["courses"] {
if let cs = courseArray as? [[NSObject:AnyObject]] {
self.courses = cs
println("\(self.courses)")
NSOperationQueue.mainQueue().addOperationWithBlock() {
self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .Automatic)
}
}
}
}
else {
if let error = error {
println("Error parsing JSON: \(error)")
} }
} else {
println("Error fetching courses: \(error.localizedDescription)")
}
}
dataTask.resume()
}
}
override func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return courses.count
}
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell",
forIndexPath: indexPath) as UITableViewCell
let course = courses[indexPath.row]
if let title: AnyObject = course["title"] {
if let titleString = title as? String {
cell.textLabel.text = titleString
}
}
return cell }
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
// Make sure the destination is a WebViewController
if let wvc = segue.destinationViewController as? WebViewController {
// Get the selected index path
if let indexPath = tableView.indexPathForSelectedRow() {
let course = courses[indexPath.row]
if let url: AnyObject = course["url"] {
if let urlString = url as? String {
wvc.URL = NSURL(string: urlString)
}
}
if let title: AnyObject = course["title"] {
if let titleString = title as? String {
wvc.navigationItem.title = titleString
}
} }
} }
}
}