質問するログイン新規登録

回答編集履歴

1

コメントへの回答

2015/05/04 14:58

投稿

simorgh3196
simorgh3196

スコア157

answer CHANGED
@@ -11,4 +11,142 @@
11
11
  ```
12
12
  などで全cellを更新させたり、他にも指定のセルだけ更新や追加させたりできるメソッドが用意されているのでそれを使えばできます。
13
13
 
14
- ちなみにUIの変更はメインスレッドで行わなければならないので注意です。
14
+ ちなみにUIの変更はメインスレッドで行わなければならないので注意です。
15
+
16
+ ----------追記----------
17
+ 解答が長く、汚くなりそうなのでこちらでコメントします。
18
+
19
+ こちらで再現しようとしてみました。
20
+ ![結果][WIDTH:600](298dc568af86d6374f2ad8a110839752.png)
21
+
22
+ storyboardの部分は上のcollectionViewのtagが1、下のcollectionViewが2です。
23
+ 残りは察してください...
24
+ ```lang-swift
25
+ import UIKit
26
+
27
+ class viewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
28
+
29
+
30
+ @IBOutlet weak var myCollectionView: UICollectionView!
31
+ @IBOutlet weak var secondCollection: UICollectionView!
32
+ var object:Array<String> = []
33
+ var object2:Array<String> = []
34
+
35
+ override func viewDidLoad() {
36
+ super.viewDidLoad()
37
+
38
+ // Cellに使われるクラスを登録.
39
+ myCollectionView.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
40
+
41
+ myCollectionView.delegate = self
42
+ myCollectionView.dataSource = self
43
+
44
+ secondCollection.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
45
+ secondCollection.delegate = self
46
+ secondCollection.dataSource = self
47
+
48
+ object = ["a","b","c"]
49
+ object2 = ["1", "2", "3", "4"]
50
+
51
+ }
52
+
53
+ /*
54
+ Cellが選択された際に呼び出される
55
+ */
56
+ func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
57
+ if collectionView.tag == 1 {
58
+ println("\(indexPath.row):\(object[indexPath.row])")
59
+ }
60
+ else if collectionView.tag == 2 {
61
+ println("\(indexPath.row):\(object2[indexPath.row])")
62
+ }
63
+ }
64
+
65
+ /*
66
+ Cellの総数を返す
67
+ */
68
+ func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
69
+ if collectionView.tag == 1 {
70
+ return object.count
71
+ }
72
+ else if collectionView.tag == 2 {
73
+ return object2.count
74
+ }
75
+ else {
76
+ println("error")
77
+ return 0
78
+ }
79
+ }
80
+
81
+ /*
82
+ Cellに値を設定する
83
+ */
84
+ func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
85
+
86
+ let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CustomUICollectionViewCell
87
+ if collectionView.tag == 1 {
88
+ cell.textLabel?.text = object[indexPath.row]
89
+ }
90
+ else if collectionView.tag == 2 {
91
+ cell.textLabel?.text = object2[indexPath.row]
92
+ }
93
+
94
+ return cell
95
+ }
96
+
97
+ @IBAction func changeData(sender: AnyObject) {
98
+ println("call change")
99
+ object[0] = "d"
100
+ object[1] = "e"
101
+ object[2] = "f"
102
+ object.append("g")
103
+
104
+ object2[0] = "10"
105
+ object2[1] = "20"
106
+ object2[2] = "30"
107
+ object2[3] = "40"
108
+ object2.append("50")
109
+
110
+ println(object)
111
+ println(object2)
112
+
113
+ }
114
+
115
+ @IBAction func reloadData(sender: AnyObject) {
116
+ println("call refresh")
117
+ self.myCollectionView.reloadData()
118
+ self.secondCollection.reloadData()
119
+ }
120
+
121
+ }
122
+
123
+ ```
124
+ ```lang-swift
125
+ import UIKit
126
+
127
+ class CustomUICollectionViewCell : UICollectionViewCell{
128
+
129
+ var textLabel : UILabel?
130
+
131
+ required init(coder aDecoder: NSCoder) {
132
+ super.init(coder: aDecoder)
133
+ }
134
+
135
+ override init(frame: CGRect) {
136
+ super.init(frame: frame)
137
+
138
+ // UILabelを生成.
139
+ textLabel = UILabel(frame: CGRectMake(0, 0, frame.width, frame.height))
140
+ textLabel?.text = "nil"
141
+ textLabel?.backgroundColor = UIColor.whiteColor()
142
+ textLabel?.textAlignment = NSTextAlignment.Center
143
+
144
+ // Cellに追加.
145
+ self.contentView.addSubview(textLabel!)
146
+ }
147
+
148
+ }
149
+ ```
150
+ 手抜き感満載ですが、こんなかんじで2つのcollectionViewを更新出来ました。
151
+ collectionViewのtagで動きを分けました。
152
+ 質問者様がどのように使っているのかわかりませんが、参考になれば。