質問編集履歴

1

collectionViewの処理を追記

2017/06/15 06:55

投稿

KazutakaShimizu
KazutakaShimizu

スコア157

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,8 @@
5
5
  やりたいこととしては、collectionViewのcellForItemAtメソッドからcell内の変数に値を渡して、その値をcellのラベルに表示するということなのですが、下記のようにcell内の変数のdidSetで処理を行うと、labelがnilの状態なのでエラーとなってしまいます。
6
6
 
7
7
  ```swift
8
+
9
+ //セルの処理
8
10
 
9
11
  class MenuBarViewCell: UICollectionViewCell {
10
12
 
@@ -26,6 +28,136 @@
26
28
 
27
29
  ```
28
30
 
31
+ ```swift
32
+
33
+ //collectionViewの処理
34
+
35
+ class MenuBarView: UIView {
36
+
37
+ var pageTabItemsWidth: CGFloat = 0.0
38
+
39
+ var collectionView:UICollectionView!
40
+
41
+ var titles: [String] = Constant.menuTitle
42
+
43
+ override init(frame: CGRect) {
44
+
45
+ super.init(frame: frame)
46
+
47
+ setUpCollectionView()
48
+
49
+ }
50
+
51
+
52
+
53
+ required init?(coder aDecoder: NSCoder) {
54
+
55
+ fatalError("init(coder:) has not been implemented")
56
+
57
+ }
58
+
59
+ }
60
+
61
+
62
+
63
+ extension MenuBarView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
64
+
65
+ fileprivate func setUpCollectionView(){
66
+
67
+ let layout = UICollectionViewFlowLayout()
68
+
69
+ layout.itemSize = CGSize(width:100, height:30)
70
+
71
+ layout.scrollDirection = .horizontal
72
+
73
+ collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height), collectionViewLayout: layout)
74
+
75
+ // Cellに使われるクラスを登録.
76
+
77
+ collectionView.delegate = self
78
+
79
+ collectionView.dataSource = self
80
+
81
+ collectionView.showsHorizontalScrollIndicator = false
82
+
83
+ collectionView.register(MenuBarViewCell.self, forCellWithReuseIdentifier: "MyCell")
84
+
85
+ self.addSubview(collectionView)
86
+
87
+
88
+
89
+ }
90
+
91
+
92
+
93
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
94
+
95
+ return titles.count * 3
96
+
97
+ }
98
+
99
+
100
+
101
+ /*
102
+
103
+ Cellに値を設定する
104
+
105
+ */
106
+
107
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
108
+
109
+ let cell : MenuBarViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell",for: indexPath) as! MenuBarViewCell
110
+
111
+ configureCell(cell: cell, indexPath: indexPath)
112
+
113
+ return cell
114
+
115
+ }
116
+
117
+
118
+
119
+ private func configureCell(cell:MenuBarViewCell, indexPath: IndexPath){
120
+
121
+ let fixedIndex = indexPath.item % titles.count
122
+
123
+ cell.title = titles[fixedIndex]
124
+
125
+ }
126
+
127
+ }
128
+
129
+
130
+
131
+ extension MenuBarView: UIScrollViewDelegate {
132
+
133
+ func scrollViewDidScroll(_ scrollView: UIScrollView) {
134
+
135
+ if pageTabItemsWidth == 0.0 {
136
+
137
+ pageTabItemsWidth = floor(scrollView.contentSize.width / 3.0) // 表示したい要素群のwidthを計算
138
+
139
+ }
140
+
141
+
142
+
143
+ if (scrollView.contentOffset.x <= 0.0) || (scrollView.contentOffset.x > pageTabItemsWidth * 2.0) { // スクロールした位置がしきい値を超えたら中央に戻す
144
+
145
+ scrollView.contentOffset.x = pageTabItemsWidth
146
+
147
+ }
148
+
149
+ }
150
+
151
+ }
152
+
153
+
154
+
155
+ ```
156
+
157
+
158
+
159
+
160
+
29
161
  イニシャライザで行っても同様です。
30
162
 
31
163