質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1956閲覧

TableViewのデータソースメソッドが呼び出されません。

jiuiuoiui

総合スコア24

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/11/12 12:19

以下のようなコードを書いたのですが、
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

通信が終了し、データを作成し終わったところでテーブルを更新reloadData()するといけるのではないでしょうか。

swift

1func SearchOkashi(){ 2 let wkStr:String = "http://www.sysbird.jp/webapi/?apikey=guest&format=json&keyword=ラムネ&max=10"; 3 let url = NSURL(string: wkStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)! 4 let config:URLSessionConfiguration = URLSessionConfiguration.default 5 let session:URLSession = URLSession(configuration: config) 6 let dataTask = session.dataTask(with: url as URL, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in 7 if (error == nil){ 8 do { 9 let json = try (JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)) as! [String:AnyObject] 10 let items = json["item"] as! [[String:AnyObject]] 11 for okashi in items { 12 self.okashiArray += [okashi["name"] as! String] 13 self.imgArray += [okashi["image"] as! String] 14 self.makerArray += [okashi["maker"] as! String] 15 } 16 17 // データを取得した後にテーブルをリロードする 18 self.table.reloadData() 19 20 } catch { 21 print("failed to parse") 22 } 23 } 24 session.invalidateAndCancel() 25 }) 26 dataTask.resume() 27}

投稿2016/11/12 12:59

編集2016/11/12 13:00
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jiuiuoiui

2016/11/12 13:02

できましたーー!ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問