質問するログイン新規登録

質問編集履歴

1

ソース

2016/02/22 12:44

投稿

sori-
sori-

スコア37

title CHANGED
File without changes
body CHANGED
@@ -29,4 +29,101 @@
29
29
  nilチェックができていないのでしょうか?
30
30
  検索しましたが、解決方法がわかりませんでした。
31
31
  解決方法をご存知の方はご教授頂けると助かります。
32
- 申し訳ございませんが、宜しくおねがいします。
32
+ 申し訳ございませんが、宜しくおねがいします。
33
+
34
+
35
+ **---追記---**
36
+ ViewController
37
+ ```swift
38
+ import UIKit
39
+
40
+ class ViewController: UITableViewController {
41
+ var entries = NSArray()
42
+
43
+ let newsUrlString = "https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://rss.itmedia.co.jp/rss/2.0/news_bursts.xml&num=8"
44
+
45
+ @IBAction func refresh(sender: AnyObject) {
46
+ let url = NSURL(string: newsUrlString)!
47
+
48
+ let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { data,response, error in
49
+
50
+ do {
51
+ // JSONデータを辞書に変換する
52
+ let dict = try NSJSONSerialization.JSONObjectWithData(data!,
53
+ options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
54
+
55
+ // /responseData/feed/entriesを取得する
56
+ guard let responseData = dict["responseData"] as? NSDictionary
57
+ else {return}
58
+ guard let feed = responseData["feed"] as? NSDictionary else {return}
59
+ guard let entries = feed["entries"] as? NSArray else {return}
60
+ self.entries = entries
61
+ } catch {}
62
+ // メインスレッドにスイッチする
63
+ dispatch_async(dispatch_get_main_queue(), {
64
+ // テーブルビューを更新する
65
+ self.tableView.reloadData()
66
+ }) //in complitionHandler
67
+ })
68
+ task.resume()
69
+ }
70
+
71
+ override func viewDidLoad() {
72
+ super.viewDidLoad()
73
+ // Do any additional setup after loading the view, typically from a nib.
74
+ }
75
+
76
+ override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
77
+ return entries.count
78
+ }
79
+
80
+ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
81
+ let cell = tableView.dequeueReusableCellWithIdentifier("news")! as
82
+ UITableViewCell
83
+
84
+ let entry = entries[indexPath.row] as! NSDictionary
85
+
86
+ cell.textLabel?.text = entry["title"] as? String
87
+
88
+ return cell
89
+ }
90
+ override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
91
+ performSegueWithIdentifier("detail", sender: entries[indexPath.row])
92
+ }
93
+
94
+ // send entry to DetailController
95
+ override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
96
+ if segue.identifier == "detail" {
97
+ // get DetailController
98
+ let detailController = segue.destinationViewController as! DetailController
99
+
100
+ // set entry
101
+ detailController.entry = sender as! NSDictionary
102
+ }
103
+ }
104
+ }
105
+ ```
106
+
107
+ DetailController
108
+ ```swift
109
+ import Foundation
110
+ import UIKit
111
+
112
+ class DetailController: UIViewController {
113
+
114
+
115
+ @IBOutlet weak var webView: UIWebView!
116
+
117
+ var entry = NSDictionary()
118
+
119
+ override func viewDidLoad() {
120
+ super.viewDidLoad()
121
+
122
+ //read URL by webView
123
+ let url = NSURL(string: self.entry["link"] as! String)!
124
+ let request = NSURLRequest(URL: url, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 60)
125
+ webView.loadRequest(request)
126
+ print(url)
127
+ }
128
+ }
129
+ ```