下記のようなコードで、APIからjsonを取得し、それを配列に格納しました。
```SWIFT
var movieList: [[String: AnyObject]] = []
func sample() { Alamofire.request(.GET, "http://123456789").validate().responseJSON { response in switch response.result { case .Success: if let value = response.result.value { let json = JSON(value) for (key, _):(String, JSON) in json["movies"] { for (key2, _):(String, JSON) in json["movies"][key] { for (_, subJson3):(String, JSON) in json["movies"][key][key2] { let movieList: [String: AnyObject] = [ "id": subJson3["id"].int!, "title": subJson3["title"].string!, "published_year": subJson3["published_year"].string! ] self.movieList.append(movieList) } } } self.tableView.reloadData() } case .Failure: print("情報を取得できませんでした。") } } }
```JSON { "movies" : { "2015" : { "1" : [ { "id" : 111, "title" : "タイトル1", "published_year" : "2015", } ], "6" : [ { "id" : 222, "title" : "タイトル2", "published_year" : "2015", } ] }, "2008" : { "3" : [ { "id" : 333, "title" : "タイトル3", "published_year" : "2008", }, { "id" : 444, "title" : "タイトル4", "published_year" : "2008", } ], }, "2016" : { "1" : [ { "id" : 555, "title" : "タイトル5", "published_year" : "2016", }, { "id" : 666, "title" : "タイトル6", "published_year" : "2016", } ] } } }
movieListの配列データの中身
```ここに言語を入力
var movieList: [[String: AnyObject]] = [["id": 111, "title": タイトル1, "published_year" : 2015], ["id": 222, "title": タイトル2, "published_year" : 2015], ["id": 333, "title": タイトル3, "published_year" : 2008], ["id": 444, "title": タイトル4, "published_year" : 2008], ["id": 555, "title": タイトル5, "published_year" : 2016], ["id": 666, "title": タイトル6, "published_year" : 2016]]
このデータからtableViewを年ごとのsectionに分けて、titleをrowに格納することは可能でしょうか? 要するに下記の画像のようにしたいです。 ![イメージ説明](19fa006dfdc1e8eefa4f6ad2a1de008b.png) もしくは、下記のような辞書型を作った方が良いのでしょうか? ```ここに言語を入力 var movieList: [String: [String: AnyObject]] = [ 2015: [["id": 111, "title": タイトル1, "published_year" : 2015], ["id": 222, "title": タイトル2, "published_year" : 2015]], 2008: [["id": 333, "title": タイトル3, "published_year" : 2008], ["id": 444, "title": タイトル4, "published_year" : 2008]], 2016: [["id": 555, "title": タイトル5, "published_year" : 2016], ["id": 666, "title": タイトル6, "published_year" : 2016]] ]
どういった配列 or 辞書型を作れば、添付画像のようなtableViewを作成することができるのでしょうか。100年分くらいのデータがありますので、変数を100年分作成するのは避けたいと思っております。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/14 05:17