###前提
Tableviewで表示したい値を代入するタイミングで、わからないことがあり質問させていただきます。
jsonファイルをhttpでリクエストしてそれを展開してtableviewに代入するということを行っています。
numberOfRowsInSectionの段階で配列に入っていないため、tableviewが表示されない現象が起きております。
imgArrayに値を追加して、tableviewを表示できるようにしたいです。
ご教授いただけますと幸いです。
何卒よろしくお願い致します。
##再現手順
1.上で変数の宣言を行う。 ⬅︎この段階では空です。
2.viewDidLoadで変数に値を入れている。
3.numberOfRowsInSectionの関数が呼び出されるタイミングでは空です。
###該当のソースコード
swift
1// 2// ViewController.swift 3// 4 5import UIKit 6import SwiftyJSON 7import Alamofire 8 9class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 10 11 var jsonURLString = "test.json" 12 //画面表示用 13 @IBOutlet weak var tableView: UITableView! 14 15 var labelArray: [String] = [] 16 var imgArray: [String] = [] 17 18 override func viewDidLoad() { 19 super.viewDidLoad() 20 // Do any additional setup after loading the view, typically from a nib. 21 22 // JSONのパース 23 Alamofire.request(.GET, jsonURLString).responseJSON { response in 24 guard let object = response.result.value else { 25 return 26 } 27 28 let json = JSON(object) 29 json.forEach { (_, json) in 30 self.labelArray.append(json["label"].string!) 31 self.imgArray.append(json["img"].string!) 32 } 33 } 34 } 35 36 override func didReceiveMemoryWarning() { 37 super.didReceiveMemoryWarning() 38 // Dispose of any resources that can be recreated. 39 } 40 41 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 42 return imgArray.count 43 } 44 45 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 46 // セルを取得する 47 let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("TimelineCell", forIndexPath: indexPath) 48 49 let img = UIImage(named:"\(imgArray[indexPath.row])") 50 let imageView = tableView.viewWithTag(1) as! UIImageView 51 imageView.image = img 52 53 // Tag番号 2 で UILabel インスタンスの生成 54 let artistName = tableView.viewWithTag(2) as! UILabel 55 artistName.text = "No.\(indexPath.row + 1)" 56 57 return cell 58 } 59 }
下記の関数が読み込まれるタイミングでは、まだ値が入っていないようです。
swift
1 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 2 return imgArray.count 3 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/07/11 11:49 編集
2016/07/11 11:57
2016/07/11 12:11 編集
退会済みユーザー
2016/07/12 05:24