http://applewatchjournal.net/development/993
を参考に現在運営しているwordpressのブログのアプリ化を行っています。
記事一覧データをrssから取得しjsonに変換、一覧表示するところでつまづいてしまっています。
具体的にはエラーはないのですが、デバッグを行った際途中で停止してしまいます。
おそらく下記のコードでバグが起きてるのかと思われます。(表示はThread 1:breakpoint 1.1)
swift
1let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell //storyboardで作成した"Cell"を取得
swiftはほぼ初心者のため原因解明ができなくて困っています。
以上、もし分かる方がいましたら回答をよろしくおねがいします。
swift
1// 2// ViewController.swift 3// zontaaku-news 4// 5// Created by yhyhyh on 2016/03/13. 6// 7// 8 9import UIKit 10import Alamofire 11 12 13class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 14 @IBOutlet weak var table: UITableView! 15 var entryDataArray = NSArray() // entriesデータを格納する配列 16 var imageCache = [String:UIImage]() //thumbnail用のimageCache 17 18override func viewDidLoad() { 19 super.viewDidLoad() 20 self.title = "ぞんたーく速報" //ViewControllerのタイトル設定 21 table.dataSource = self 22 table.delegate = self 23 24 let now = NSDate() 25 let formatter = NSDateFormatter() 26 formatter.dateFormat = "yyyy/MM/ddHH:mm" 27 _ = formatter.stringFromDate(now) 28 29 let requestUrl = "https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://xn--y8j3ad1snt559pu2ve.com/feed/&num=20" 30 31 Alamofire.request(.GET, requestUrl, parameters:["foo": "bar"]) 32 .responseJSON { response in 33 debugPrint(response) 34 35 print(response.request) 36 print(response.response) 37 print(response.data) 38 print(response.result) 39 40 if let JSON = response.result.value { 41 42 let jsonDic = JSON as! NSDictionary 43 let responseData = jsonDic["responseData"] as! NSDictionary 44 let feed = responseData["feed"] as! NSDictionary 45 self.entryDataArray = feed["entries"] as! NSArray 46 47 /* print("JSON: \(JSON)") */ 48 print("self.entryDataArray: \(self.entryDataArray)") 49 50 self.entryDataArray = feed["entries"] as! NSArray 51 self.table.reloadData() //取得したデータをテーブルビューに表示 52 53 } 54 } 55 } 56 57 58 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 59 return entryDataArray.count 60 } 61 62 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 63 let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell //storyboardで作成した"Cell"を取得 64 65 let entryDic = entryDataArray[indexPath.row] as! NSDictionary //配列の"indexPath.row!"番目の要を取得 66 67 // 取得した要素からタイトル、掲載日、カテゴリー(複数選択している場合は最初のカテゴリー)をCellにセット 68 cell.title.text = entryDic["title"] as? String 69 cell.date.text = entryDic["publishedDate"] as? String 70 let category = entryDic["categories"] as! NSArray 71 cell.category.text = category[0] as? String 72 73 // JSONデータの中から、thumbnail画像のURLを取得し、NSURL型に変換(ゴリ押し処理) 74 var mediaGroups:NSArray = (entryDic["mediaGroups"] as? NSArray)! 75 var mediacontents:NSDictionary = (mediaGroups[0] as? NSDictionary)! 76 mediaGroups = (mediacontents["contents"] as? NSArray)! 77 mediacontents = (mediaGroups[0] as? NSDictionary)! 78 let urlString = mediacontents["url"] as! String 79 let imgURL: NSURL? = NSURL(string: urlString) 80 81 // キャッシュがあればキャッシュを表示、なければ非同期通信で画像データを取得して表示 82 if let img = imageCache[urlString] { 83 cell.thumbnail?.image = img 84 } 85 else{ 86 let request: NSURLRequest = NSURLRequest(URL: imgURL!) 87 let mainQueue = NSOperationQueue.mainQueue() 88 NSURLConnection.sendAsynchronousRequest(request, queue: mainQueue, completionHandler: { (response, data, error) -> Void in 89 if error == nil { 90 let image = UIImage(data: data!) 91 self.imageCache[urlString] = image 92 dispatch_async(dispatch_get_main_queue(), { 93 cell.thumbnail?.image = image 94 }) 95 } 96 else { 97 print("Error: \(error!.localizedDescription)") 98 } 99 }) 100 } 101 return cell 102 } 103 104 105 106 override func didReceiveMemoryWarning() { 107 super.didReceiveMemoryWarning() 108 // Dispose of any resources that can be recreated. 109 } 110 111 112} 113 114
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。