以下のようなコードを書いたのですが、
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
が呼び出されません。
もちろん、storyboardでdatasourceとdelegateの設定はしてあります。
Swift
1 2import UIKit 3 4class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 5 6 @IBOutlet weak var mySearchBar: UISearchBar! 7 @IBOutlet var table: UITableView! 8 9 10 var imgArray: [String] = [] 11 var okashiArray: [String] = [] 12 var makerArray: [String] = [] 13 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 SearchOkashi() 18 19 } 20 21 override func didReceiveMemoryWarning() { 22 super.didReceiveMemoryWarning() 23 24 } 25 26 func SearchOkashi(){ 27 let wkStr:String = "http://www.sysbird.jp/webapi/?apikey=guest&format=json&keyword=ラムネ&max=10"; 28 let url = NSURL(string: wkStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)! 29 let config:URLSessionConfiguration = URLSessionConfiguration.default 30 let session:URLSession = URLSession(configuration: config) 31 let dataTask = session.dataTask(with: url as URL, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in 32 if (error == nil){ 33 do { 34 let json = try (JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)) as! [String:AnyObject] 35 let items = json["item"] as! [[String:AnyObject]] 36 for okashi in items { 37 self.okashiArray += [okashi["name"] as! String] 38 self.imgArray += [okashi["image"] as! String] 39 self.makerArray += [okashi["maker"] as! String] 40 } 41 } catch { 42 print("failed to parse") 43 } 44 } 45 session.invalidateAndCancel() 46 }) 47 dataTask.resume() 48 } 49 50 func tableView(_ table: UITableView, numberOfRowsInSection section: Int) -> Int { 51 return imgArray.count 52 } 53 54 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 55 print("test") 56 // tableCell の ID で UITableViewCell のインスタンスを生成 57 let cell = table.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) 58 59 // Tag番号 1 で UIImageView インスタンスの生成 60 let imageView = table.viewWithTag(1) as! UIImageView 61 let url = NSURL(string:"\(imgArray[indexPath.row])") 62 let req = NSURLRequest(url:url! as URL) 63 64 NSURLConnection.sendAsynchronousRequest(req as URLRequest, queue:OperationQueue.main){(res, data, err) in 65 66 let image = UIImage(data:data!) 67 imageView.image = image 68 } 69 70 // Tag番号 2 で UILabel インスタンスの生成 71 let label1 = table.viewWithTag(2) as! UILabel 72 label1.text = "\(okashiArray[indexPath.row])" 73 // Tag番号 3 で UILabel インスタンスの生成 74 let label2 = table.viewWithTag(3) as! UILabel 75 label2.text = "\(makerArray[indexPath.row])" 76 77 return cell 78 79 } 80} 81
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/12 13:02