質問編集履歴
4
タイトル変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
最適な配列 or 辞書型を作成して、添付画像のようなtableViewを作成したい
|
test
CHANGED
File without changes
|
3
質問を詳しく記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,85 +1,205 @@
|
|
1
|
+
下記のようなコードで、APIからjsonを取得し、それを配列に格納しました。
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
```SWIFT
|
6
|
+
|
7
|
+
var movieList: [[String: AnyObject]] = []
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
func sample() {
|
12
|
+
|
13
|
+
Alamofire.request(.GET, "http://123456789").validate().responseJSON { response in
|
14
|
+
|
15
|
+
switch response.result {
|
16
|
+
|
17
|
+
case .Success:
|
18
|
+
|
19
|
+
if let value = response.result.value {
|
20
|
+
|
21
|
+
let json = JSON(value)
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
for (key, _):(String, JSON) in json["movies"] {
|
26
|
+
|
27
|
+
for (key2, _):(String, JSON) in json["movies"][key] {
|
28
|
+
|
1
|
-
s
|
29
|
+
for (_, subJson3):(String, JSON) in json["movies"][key][key2] {
|
2
|
-
|
30
|
+
|
3
|
-
|
31
|
+
let movieList: [String: AnyObject] = [
|
4
|
-
|
5
|
-
|
6
|
-
|
32
|
+
|
7
|
-
|
33
|
+
"id": subJson3["id"].int!,
|
34
|
+
|
8
|
-
|
35
|
+
"title": subJson3["title"].string!,
|
36
|
+
|
9
|
-
|
37
|
+
"published_year": subJson3["published_year"].string!
|
38
|
+
|
10
|
-
|
39
|
+
]
|
40
|
+
|
41
|
+
self.movieList.append(movieList)
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
self.tableView.reloadData()
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
case .Failure:
|
54
|
+
|
55
|
+
print("情報を取得できませんでした。")
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
}
|
62
|
+
|
11
|
-
```
|
63
|
+
```
|
12
|
-
|
64
|
+
|
65
|
+
|
66
|
+
|
13
|
-
|
67
|
+
```JSON
|
14
|
-
|
68
|
+
|
15
|
-
|
69
|
+
{
|
70
|
+
|
16
|
-
|
71
|
+
"movies" : {
|
72
|
+
|
73
|
+
"2015" : {
|
74
|
+
|
75
|
+
"1" : [
|
76
|
+
|
77
|
+
{
|
78
|
+
|
17
|
-
"id" : 1
|
79
|
+
"id" : 111,
|
18
|
-
|
80
|
+
|
19
|
-
"title" : "タイトル1",
|
81
|
+
"title" : "タイトル1",
|
82
|
+
|
20
|
-
|
83
|
+
"published_year" : "2015",
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
],
|
88
|
+
|
89
|
+
"6" : [
|
90
|
+
|
91
|
+
{
|
92
|
+
|
93
|
+
"id" : 222,
|
94
|
+
|
21
|
-
"
|
95
|
+
"title" : "タイトル2",
|
96
|
+
|
97
|
+
"published_year" : "2015",
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
]
|
22
102
|
|
23
103
|
},
|
24
104
|
|
105
|
+
"2008" : {
|
106
|
+
|
107
|
+
"3" : [
|
108
|
+
|
25
|
-
{
|
109
|
+
{
|
26
|
-
|
110
|
+
|
27
|
-
"id" :
|
111
|
+
"id" : 333,
|
28
|
-
|
112
|
+
|
29
|
-
"title" : "タイトル
|
113
|
+
"title" : "タイトル3",
|
30
|
-
|
114
|
+
|
31
|
-
"year" : "2008
|
115
|
+
"published_year" : "2008",
|
116
|
+
|
117
|
+
},
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
"id" : 444,
|
122
|
+
|
123
|
+
"title" : "タイトル4",
|
124
|
+
|
125
|
+
"published_year" : "2008",
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
],
|
130
|
+
|
131
|
+
},
|
132
|
+
|
133
|
+
"2016" : {
|
134
|
+
|
135
|
+
"1" : [
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
"id" : 555,
|
140
|
+
|
141
|
+
"title" : "タイトル5",
|
142
|
+
|
143
|
+
"published_year" : "2016",
|
144
|
+
|
145
|
+
},
|
146
|
+
|
147
|
+
{
|
148
|
+
|
149
|
+
"id" : 666,
|
150
|
+
|
151
|
+
"title" : "タイトル6",
|
152
|
+
|
153
|
+
"published_year" : "2016",
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
]
|
32
158
|
|
33
159
|
}
|
34
160
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
}
|
82
|
-
|
83
|
-
]
|
84
|
-
|
85
|
-
}```
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
**movieListの配列データの中身**
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
```ここに言語を入力
|
172
|
+
|
173
|
+
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]]
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
このデータからtableViewを年ごとのsectionに分けて、titleをrowに格納することは可能でしょうか?
|
180
|
+
|
181
|
+
要するに下記の画像のようにしたいです。
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
![イメージ説明](19fa006dfdc1e8eefa4f6ad2a1de008b.png)
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
もしくは、下記のような辞書型を作った方が良いのでしょうか?
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
```ここに言語を入力
|
194
|
+
|
195
|
+
var movieList: [String: [String: AnyObject]] = [
|
196
|
+
|
197
|
+
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]]
|
198
|
+
|
199
|
+
]
|
200
|
+
|
201
|
+
```
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
どういった配列 or 辞書型を作れば、添付画像のようなtableViewを作成することができるのでしょうか。100年分くらいのデータがありますので、変数を100年分作成するのは避けたいと思っております。
|
2
文章追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
swiftで以下のようなjsonデータを使って、添付画像のようなtableViewを作成したいのですが、どういったデータを作成すれば良いでしょうか?
|
2
|
+
|
3
|
+
いろいろな年の値が返ってくるので、どんな値が返ってきても対応できるようにしたいです。
|
2
4
|
|
3
5
|
|
4
6
|
|
1
画像変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
![イメージ説明](
|
5
|
+
![イメージ説明](657bda6d84ac24e45752936d53c7e256.png)
|
6
6
|
|
7
7
|
|
8
8
|
|