-1
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    let label = cell.contentView.viewWithTag(1) as? UILabel
    let photo = cell.contentView.viewWithTag(3) as? UIImageView
    label.text = articles[indexPath.row]["name"] as? String ←※ここでエラー

    if let star = cell.contentView.viewWithTag(starTag) as? AXRatingView {
        star.value = 1.5
    } else {
        star(cell, photo: photo!)
    }

    return cell
}

func getData() {

    Alamofire.request(.GET, "http://sample.jp/api", headers: appDelegate.customHeader)
        .responseJSON { respone in
            guard let object = respone.result.value else {
                print("取得に失敗しました")
                return
            }
            self.articles = []
            let json = JSON(object)
            json["result"]["object"].forEach { (_, json) in
                let article: [String: AnyObject?] = [
                    "id" : json["id"].string!,
                    "name": json["name"].string!,
                    "start_date": json["start"]["date"].string!,
                    "end_date": json["end"]["date"].string!
                ]
                self.articles.append(article)
            }
            self.myTable.reloadData()
    }

}

上記のコードでviewDidLoad内でgetDataメソッドを呼び出しているのですが
どうも情報の取得より先にテーブルの生成が行われ、nilを代入しようとしエラーが出てしまいます。

似た処理を他のViewで処理しようとした際はうまくいっています。
どうしたら情報の取得後にテーブルの生成に移れるでしょうか。


こちらがうまくいく側のコードです

func tableView(table: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {

    appDelegate.selectedId = (articles[indexPath.row]["id"] as? String)!

}

func getAllData() {

    Alamofire.request(.GET, "http://sample.jp/api/search", headers: appDelegate.customHeader)
        .responseJSON { respone in
            guard let object = respone.result.value else {
                print("一覧の取得に失敗しました")
                return
            }
            self.articles = []
            let json = JSON(object)
            json["result"].forEach { (_, json) in
                let article: [String: AnyObject?] = [
                    "id" : json["id"].string!,
                    "name": json["name"].string!,
                    "start_date": json["start"]["date"].string!,
                    "end_date": json["end"]["date"].string!
                ]
                self.articles.append(article)
            }
            self.myTable.reloadData()
    }

}

1 件の回答 1

1

2案あります
・遷移前の画面からデータを渡す
・numberOfRowsInSectionで一旦0を返し、データ取得後に reloadDataを行う

4
  • 「numberOfRowsInSectionで一旦0を返し、データ取得後に reloadDataを行う」について教えていただけないでしょうか
    – Y_M
    2016年7月8日 3:24
  • 例えば articlesは初期化しておく articles = [Article]() これでnumberOfRowsInSectionでは0返されます。 viewDidLoadでgetAllDataを呼び出せばよいと思います。
    – Porinnki
    2016年7月12日 4:24
  • その場合getAllDataの最後でreloadDataを行うという認識で問題ないでしょうか。
    – Y_M
    2016年7月12日 7:06
  • はい。self.articles.append(article) で値が入ったあとにreloadDataをすることで、再度numberOfRowsInSectionが呼ばれるはずです
    – Porinnki
    2016年7月12日 9:39

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。