質問編集履歴

1

ソース

2016/02/22 12:44

投稿

sori-
sori-

スコア37

test CHANGED
File without changes
test CHANGED
@@ -61,3 +61,197 @@
61
61
  解決方法をご存知の方はご教授頂けると助かります。
62
62
 
63
63
  申し訳ございませんが、宜しくおねがいします。
64
+
65
+
66
+
67
+
68
+
69
+ **---追記---**
70
+
71
+ ViewController
72
+
73
+ ```swift
74
+
75
+ import UIKit
76
+
77
+
78
+
79
+ class ViewController: UITableViewController {
80
+
81
+ var entries = NSArray()
82
+
83
+
84
+
85
+ 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"
86
+
87
+
88
+
89
+ @IBAction func refresh(sender: AnyObject) {
90
+
91
+ let url = NSURL(string: newsUrlString)!
92
+
93
+
94
+
95
+ let task = NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { data,response, error in
96
+
97
+
98
+
99
+ do {
100
+
101
+ // JSONデータを辞書に変換する
102
+
103
+ let dict = try NSJSONSerialization.JSONObjectWithData(data!,
104
+
105
+ options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
106
+
107
+
108
+
109
+ // /responseData/feed/entriesを取得する
110
+
111
+ guard let responseData = dict["responseData"] as? NSDictionary
112
+
113
+ else {return}
114
+
115
+ guard let feed = responseData["feed"] as? NSDictionary else {return}
116
+
117
+ guard let entries = feed["entries"] as? NSArray else {return}
118
+
119
+ self.entries = entries
120
+
121
+ } catch {}
122
+
123
+ // メインスレッドにスイッチする
124
+
125
+ dispatch_async(dispatch_get_main_queue(), {
126
+
127
+ // テーブルビューを更新する
128
+
129
+ self.tableView.reloadData()
130
+
131
+ }) //in complitionHandler
132
+
133
+ })
134
+
135
+ task.resume()
136
+
137
+ }
138
+
139
+
140
+
141
+ override func viewDidLoad() {
142
+
143
+ super.viewDidLoad()
144
+
145
+ // Do any additional setup after loading the view, typically from a nib.
146
+
147
+ }
148
+
149
+
150
+
151
+ override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
152
+
153
+ return entries.count
154
+
155
+ }
156
+
157
+
158
+
159
+ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
160
+
161
+ let cell = tableView.dequeueReusableCellWithIdentifier("news")! as
162
+
163
+ UITableViewCell
164
+
165
+
166
+
167
+ let entry = entries[indexPath.row] as! NSDictionary
168
+
169
+
170
+
171
+ cell.textLabel?.text = entry["title"] as? String
172
+
173
+
174
+
175
+ return cell
176
+
177
+ }
178
+
179
+ override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
180
+
181
+ performSegueWithIdentifier("detail", sender: entries[indexPath.row])
182
+
183
+ }
184
+
185
+
186
+
187
+ // send entry to DetailController
188
+
189
+ override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
190
+
191
+ if segue.identifier == "detail" {
192
+
193
+ // get DetailController
194
+
195
+ let detailController = segue.destinationViewController as! DetailController
196
+
197
+
198
+
199
+ // set entry
200
+
201
+ detailController.entry = sender as! NSDictionary
202
+
203
+ }
204
+
205
+ }
206
+
207
+ }
208
+
209
+ ```
210
+
211
+
212
+
213
+ DetailController
214
+
215
+ ```swift
216
+
217
+ import Foundation
218
+
219
+ import UIKit
220
+
221
+
222
+
223
+ class DetailController: UIViewController {
224
+
225
+
226
+
227
+
228
+
229
+ @IBOutlet weak var webView: UIWebView!
230
+
231
+
232
+
233
+ var entry = NSDictionary()
234
+
235
+
236
+
237
+ override func viewDidLoad() {
238
+
239
+ super.viewDidLoad()
240
+
241
+
242
+
243
+ //read URL by webView
244
+
245
+ let url = NSURL(string: self.entry["link"] as! String)!
246
+
247
+ let request = NSURLRequest(URL: url, cachePolicy: .ReloadIgnoringLocalCacheData, timeoutInterval: 60)
248
+
249
+ webView.loadRequest(request)
250
+
251
+ print(url)
252
+
253
+ }
254
+
255
+ }
256
+
257
+ ```