jsonデータを取得することには成功しているのですが、セルがうまく実装されません。
storyboard
imageview,labelにはそれぞれtagをつけてあります。
エラーはでません
import UIKit class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { @IBOutlet weak var mySearchBar: UISearchBar! @IBOutlet weak var table: UITableView! var imgArray: [String] = [] var okashiArray: [String] = [] var makerArray: [String] = [] override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func SearchOkashi(){ //let url:NSURL = NSURL(string: "http://www.sysbird.jp/webapi/?apikey=guest&format=json&keyword=ラムネ&max=10")! let wkStr:String = "http://www.sysbird.jp/webapi/?apikey=guest&format=json&keyword=ラムネ&max=10"; let url = NSURL(string: wkStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)! let config:URLSessionConfiguration = URLSessionConfiguration.default let session:URLSession = URLSession(configuration: config) let dataTask = session.dataTask(with: url as URL, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in if (error == nil){ do { let json = try (JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)) as! [String:AnyObject] let items = json["item"] as! [[String:AnyObject]] for okashi in items { self.okashiArray += [okashi["name"] as! String] self.imgArray += [okashi["image"] as! String] print(okashi["image"] as! String) self.makerArray += [okashi["maker"] as! String] print(okashi["maker"] as! String) } } catch { print("failed to parse") } } session.invalidateAndCancel() }) dataTask.resume() } func tableView(_ table: UITableView, numberOfRowsInSection section: Int) -> Int { SearchOkashi() return imgArray.count } func tableView(_ table: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // tableCell の ID で UITableViewCell のインスタンスを生成 let cell = table.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) // Tag番号 1 で UIImageView インスタンスの生成 let imageView = table.viewWithTag(1) as! UIImageView let url = NSURL(string:"\(imgArray[indexPath.row])") let req = NSURLRequest(url:url! as URL) NSURLConnection.sendAsynchronousRequest(req as URLRequest, queue:OperationQueue.main){(res, data, err) in let image = UIImage(data:data!) imageView.image = image } // Tag番号 2 で UILabel インスタンスの生成 let label1 = table.viewWithTag(2) as! UILabel label1.text = "\(okashiArray[indexPath.row])" print(okashiArray.count) // Tag番号 3 で UILabel インスタンスの生成 let label2 = table.viewWithTag(3) as! UILabel label2.text = "\(makerArray[indexPath.row])" retu![イメージ説明](4d5377524146979b29157429a6928efe.png)l } }
###試したこと
tableViewのセルの数を返すメソッドの中でsearchOkashi()をしてみたのですがだめでした。
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。