以下のURLのjsonデータから、各キャラクターのidとtitleを取り出したいのですが、どのようにすればいいのかわかりません。
リンク内容
以下のようなコードを書いてみましたが、データの解析のところでかなり間違いがあると思います..
Swift
1import UIKit 2 3class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,URLSessionDelegate { 4 5 var characters: [String] = [] 6 var explains: [String] = [] 7 8 @IBOutlet weak var myTable: UITableView! 9 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 setuptableview() 14 // Do any additional setup after loading the view, typically from a nib. 15 } 16 17 override func didReceiveMemoryWarning() { 18 super.didReceiveMemoryWarning() 19 // Dispose of any resources that can be recreated. 20 } 21 22 func setuptableview(){ 23 let url: NSURL = NSURL(string: "https://global.api.pvp.net/api/lol/static-data/jp/v1.2/champion?api_key=RGAPI-40046844-78a7-43b4-81af-caedf4a701d1")! 24 let configuration: URLSessionConfiguration = URLSessionConfiguration.default 25 let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil) 26 27 let task: URLSessionDataTask = session.dataTask(with: url as URL, completionHandler: {(data, response, err) -> Void in 28 if data != nil{ 29 let str = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 30 print(str!) 31 32 do{ 33 let json: NSDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary 34 let champions = json["data"] as! [[String:AnyObject]] 35 36 for championdates in champions { 37 self.characters.append(championdates["key"] as! String) 38 self.explains.append(championdates["title"] as! String) 39 } 40 41 }catch{ 42 print("error") 43 print(error) 44 } 45 } 46 }) 47 task.resume() 48 } 49 50 func tableView(_ table: UITableView, numberOfRowsInSection section: Int) -> Int { 51 return characters.count 52 } 53 54 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 55 print("test") 56 // tableCell の ID で UITableViewCell のインスタンスを生成 57 let cell = myTable.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) 58 59 // Tag番号 1 で UIImageView インスタンスの生成 60 let imageView = myTable.viewWithTag(1) as! UIImageView 61 let url = NSURL(string:"http://ddragon.leagueoflegends.com/cdn/6.22.1/img/champion/\(characters[indexPath.row]).png") 62 let req = NSURLRequest(url: url! as URL,cachePolicy: .returnCacheDataElseLoad,timeoutInterval: 5*60) 63 let conf = URLSessionConfiguration.default; 64 let session = URLSession(configuration: conf, delegate: nil, delegateQueue: OperationQueue.main) 65 66 session.dataTask(with: req as URLRequest, completionHandler: 67 { (data, resp, err) in 68 if((err) == nil){ //Success 69 let image = UIImage(data:data!) 70 imageView.image = image 71 }else{ //Error 72 print("AsyncImageView:Error \(err?.localizedDescription)"); 73 } 74 self.myTable.reloadData() 75 }).resume(); 76 77 78 79 // Tag番号 2 で UILabel インスタンスの生成 80 let label1 = myTable.viewWithTag(2) as! UILabel 81 label1.text = "\(characters[indexPath.row])" 82 // Tag番号 3 で UILabel インスタンスの生成 83 let label2 = myTable.viewWithTag(3) as! UILabel 84 label2.text = "\(explains[indexPath.row])" 85 86 return cell 87 88 } 89} 90
回答お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/16 07:52
2016/11/16 08:28