回答編集履歴

2

追記しました。

2018/04/19 21:25

投稿

newmt
newmt

スコア1277

test CHANGED
@@ -115,3 +115,173 @@
115
115
  }
116
116
 
117
117
  ```
118
+
119
+
120
+
121
+ 【再追記】
122
+
123
+
124
+
125
+ 例えば下記の箇所を変更するといかがでしょうか?
126
+
127
+
128
+
129
+
130
+
131
+ numberOfItemsInSectionでは1を返します。
132
+
133
+
134
+
135
+ ```
136
+
137
+ override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
138
+
139
+ return 1
140
+
141
+ }
142
+
143
+ ```
144
+
145
+
146
+
147
+ sizeForItemAtでセルが1個になるのでサイズを少し大きくする(これは任意です。)
148
+
149
+
150
+
151
+ ```
152
+
153
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
154
+
155
+
156
+
157
+ switch indexPath.section {
158
+
159
+ case 2:
160
+
161
+ // セルのサイズを少し大きくしています。
162
+
163
+ return CGSize(width: 300, height: 100)
164
+
165
+ case 1:
166
+
167
+ return CGSize(width: view.frame.width, height: 200)
168
+
169
+ default:
170
+
171
+ return CGSize(width: view.frame.width, height: 200)
172
+
173
+ }
174
+
175
+ }
176
+
177
+ ```
178
+
179
+
180
+
181
+ CCCCell内でcollectionViewを作成する。
182
+
183
+
184
+
185
+ ```
186
+
187
+ import UIKit
188
+
189
+
190
+
191
+ class CCCCell: UICollectionViewCell, UICollectionViewDataSource {
192
+
193
+
194
+
195
+ var collectionView: UICollectionView?
196
+
197
+
198
+
199
+ override init(frame: CGRect) {
200
+
201
+ super.init(frame: frame)
202
+
203
+ setupMenuBar()
204
+
205
+ backgroundColor = .gray
206
+
207
+
208
+
209
+ }
210
+
211
+
212
+
213
+ required init?(coder aDecoder: NSCoder) {
214
+
215
+ fatalError("init(coder:) has not been implemented")
216
+
217
+ }
218
+
219
+
220
+
221
+ private func setupMenuBar() {
222
+
223
+ let layout = UICollectionViewFlowLayout()
224
+
225
+ layout.scrollDirection = .horizontal
226
+
227
+ collectionView = UICollectionView(frame: self.contentView.frame, collectionViewLayout: layout)
228
+
229
+
230
+
231
+ collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
232
+
233
+ collectionView?.dataSource = self
234
+
235
+
236
+
237
+
238
+
239
+ self.addSubview(collectionView!)
240
+
241
+ }
242
+
243
+
244
+
245
+ func numberOfSections(in collectionView: UICollectionView) -> Int {
246
+
247
+ return 1
248
+
249
+ }
250
+
251
+
252
+
253
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
254
+
255
+ return 20
256
+
257
+ }
258
+
259
+
260
+
261
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
262
+
263
+
264
+
265
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
266
+
267
+ cell.backgroundColor = .yellow
268
+
269
+ return cell
270
+
271
+ }
272
+
273
+
274
+
275
+
276
+
277
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
278
+
279
+
280
+
281
+ return UIEdgeInsetsMake(50, 50, 50, 50)
282
+
283
+ }
284
+
285
+ }
286
+
287
+ ```

1

追記しました

2018/04/19 21:25

投稿

newmt
newmt

スコア1277

test CHANGED
@@ -1 +1,117 @@
1
1
  グレーの部分のセルにUICollectionViewが設定されているように見えますので、そのUICollectionViewのUICollectionViewFlowLayoutで指定すればできると思います。
2
+
3
+
4
+
5
+ 【追記】
6
+
7
+
8
+
9
+ 例えばaCellというカスタムセルの中にUICollectionViewがあるようなイメージだといかがでしょうか?
10
+
11
+
12
+
13
+ ```
14
+
15
+ class aCell: UICollectionViewCell, UICollectionViewDataSource {
16
+
17
+
18
+
19
+ var collectionView: UICollectionView?
20
+
21
+
22
+
23
+ required init(coder aDecoder: NSCoder) {
24
+
25
+
26
+
27
+ super.init(coder: aDecoder)!
28
+
29
+ setup()
30
+
31
+
32
+
33
+ }
34
+
35
+
36
+
37
+ override init(frame: CGRect) {
38
+
39
+ super.init(frame: frame)
40
+
41
+ setup()
42
+
43
+ }
44
+
45
+
46
+
47
+ private func setup() {
48
+
49
+
50
+
51
+ let layout = UICollectionViewFlowLayout()
52
+
53
+ layout.scrollDirection = .horizontal
54
+
55
+ collectionView = UICollectionView(frame: self.contentView.frame, collectionViewLayout: layout)
56
+
57
+
58
+
59
+ collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
60
+
61
+ collectionView?.dataSource = self
62
+
63
+
64
+
65
+
66
+
67
+ self.addSubview(collectionView!)
68
+
69
+
70
+
71
+ }
72
+
73
+
74
+
75
+ func numberOfSections(in collectionView: UICollectionView) -> Int {
76
+
77
+ return 1
78
+
79
+ }
80
+
81
+
82
+
83
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
84
+
85
+ return 20
86
+
87
+ }
88
+
89
+
90
+
91
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
92
+
93
+
94
+
95
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
96
+
97
+ cell.backgroundColor = .yellow
98
+
99
+ return cell
100
+
101
+ }
102
+
103
+
104
+
105
+
106
+
107
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
108
+
109
+
110
+
111
+ return UIEdgeInsetsMake(50, 50, 50, 50)
112
+
113
+ }
114
+
115
+ }
116
+
117
+ ```