回答編集履歴

1

コメントへの回答

2015/05/04 14:58

投稿

simorgh3196
simorgh3196

スコア157

test CHANGED
@@ -25,3 +25,279 @@
25
25
 
26
26
 
27
27
  ちなみにUIの変更はメインスレッドで行わなければならないので注意です。
28
+
29
+
30
+
31
+ ----------追記----------
32
+
33
+ 解答が長く、汚くなりそうなのでこちらでコメントします。
34
+
35
+
36
+
37
+ こちらで再現しようとしてみました。
38
+
39
+ ![結果][WIDTH:600](298dc568af86d6374f2ad8a110839752.png)
40
+
41
+
42
+
43
+ storyboardの部分は上のcollectionViewのtagが1、下のcollectionViewが2です。
44
+
45
+ 残りは察してください...
46
+
47
+ ```lang-swift
48
+
49
+ import UIKit
50
+
51
+
52
+
53
+ class viewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
54
+
55
+
56
+
57
+
58
+
59
+ @IBOutlet weak var myCollectionView: UICollectionView!
60
+
61
+ @IBOutlet weak var secondCollection: UICollectionView!
62
+
63
+ var object:Array<String> = []
64
+
65
+ var object2:Array<String> = []
66
+
67
+
68
+
69
+ override func viewDidLoad() {
70
+
71
+ super.viewDidLoad()
72
+
73
+
74
+
75
+ // Cellに使われるクラスを登録.
76
+
77
+ myCollectionView.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
78
+
79
+
80
+
81
+ myCollectionView.delegate = self
82
+
83
+ myCollectionView.dataSource = self
84
+
85
+
86
+
87
+ secondCollection.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
88
+
89
+ secondCollection.delegate = self
90
+
91
+ secondCollection.dataSource = self
92
+
93
+
94
+
95
+ object = ["a","b","c"]
96
+
97
+ object2 = ["1", "2", "3", "4"]
98
+
99
+
100
+
101
+ }
102
+
103
+
104
+
105
+ /*
106
+
107
+ Cellが選択された際に呼び出される
108
+
109
+ */
110
+
111
+ func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
112
+
113
+ if collectionView.tag == 1 {
114
+
115
+ println("\(indexPath.row):\(object[indexPath.row])")
116
+
117
+ }
118
+
119
+ else if collectionView.tag == 2 {
120
+
121
+ println("\(indexPath.row):\(object2[indexPath.row])")
122
+
123
+ }
124
+
125
+ }
126
+
127
+
128
+
129
+ /*
130
+
131
+ Cellの総数を返す
132
+
133
+ */
134
+
135
+ func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
136
+
137
+ if collectionView.tag == 1 {
138
+
139
+ return object.count
140
+
141
+ }
142
+
143
+ else if collectionView.tag == 2 {
144
+
145
+ return object2.count
146
+
147
+ }
148
+
149
+ else {
150
+
151
+ println("error")
152
+
153
+ return 0
154
+
155
+ }
156
+
157
+ }
158
+
159
+
160
+
161
+ /*
162
+
163
+ Cellに値を設定する
164
+
165
+ */
166
+
167
+ func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
168
+
169
+
170
+
171
+ let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CustomUICollectionViewCell
172
+
173
+ if collectionView.tag == 1 {
174
+
175
+ cell.textLabel?.text = object[indexPath.row]
176
+
177
+ }
178
+
179
+ else if collectionView.tag == 2 {
180
+
181
+ cell.textLabel?.text = object2[indexPath.row]
182
+
183
+ }
184
+
185
+
186
+
187
+ return cell
188
+
189
+ }
190
+
191
+
192
+
193
+ @IBAction func changeData(sender: AnyObject) {
194
+
195
+ println("call change")
196
+
197
+ object[0] = "d"
198
+
199
+ object[1] = "e"
200
+
201
+ object[2] = "f"
202
+
203
+ object.append("g")
204
+
205
+
206
+
207
+ object2[0] = "10"
208
+
209
+ object2[1] = "20"
210
+
211
+ object2[2] = "30"
212
+
213
+ object2[3] = "40"
214
+
215
+ object2.append("50")
216
+
217
+
218
+
219
+ println(object)
220
+
221
+ println(object2)
222
+
223
+
224
+
225
+ }
226
+
227
+
228
+
229
+ @IBAction func reloadData(sender: AnyObject) {
230
+
231
+ println("call refresh")
232
+
233
+ self.myCollectionView.reloadData()
234
+
235
+ self.secondCollection.reloadData()
236
+
237
+ }
238
+
239
+
240
+
241
+ }
242
+
243
+
244
+
245
+ ```
246
+
247
+ ```lang-swift
248
+
249
+ import UIKit
250
+
251
+
252
+
253
+ class CustomUICollectionViewCell : UICollectionViewCell{
254
+
255
+
256
+
257
+ var textLabel : UILabel?
258
+
259
+
260
+
261
+ required init(coder aDecoder: NSCoder) {
262
+
263
+ super.init(coder: aDecoder)
264
+
265
+ }
266
+
267
+
268
+
269
+ override init(frame: CGRect) {
270
+
271
+ super.init(frame: frame)
272
+
273
+
274
+
275
+ // UILabelを生成.
276
+
277
+ textLabel = UILabel(frame: CGRectMake(0, 0, frame.width, frame.height))
278
+
279
+ textLabel?.text = "nil"
280
+
281
+ textLabel?.backgroundColor = UIColor.whiteColor()
282
+
283
+ textLabel?.textAlignment = NSTextAlignment.Center
284
+
285
+
286
+
287
+ // Cellに追加.
288
+
289
+ self.contentView.addSubview(textLabel!)
290
+
291
+ }
292
+
293
+
294
+
295
+ }
296
+
297
+ ```
298
+
299
+ 手抜き感満載ですが、こんなかんじで2つのcollectionViewを更新出来ました。
300
+
301
+ collectionViewのtagで動きを分けました。
302
+
303
+ 質問者様がどのように使っているのかわかりませんが、参考になれば。