質問編集履歴

1

画像の変更、コードの修正

2020/06/05 06:00

投稿

nishimu
nishimu

スコア26

test CHANGED
File without changes
test CHANGED
@@ -60,268 +60,280 @@
60
60
 
61
61
  super.viewDidLoad()
62
62
 
63
+
64
+
65
+ self.view.backgroundColor = UIColor.red
66
+
67
+ self.collectionView.backgroundColor = UIColor.blue
68
+
69
+ collectionView.register(CollectionViewCell_D.self, forCellWithReuseIdentifier: "CollectionViewCell_D")
70
+
71
+ collectionView.register(CollectionViewCell_S.self, forCellWithReuseIdentifier: "CollectionViewCell_S")
72
+
73
+ let layout = UICollectionViewFlowLayout()
74
+
75
+ layout.scrollDirection = .vertical
76
+
63
77
 
64
78
 
79
+ }
80
+
81
+
82
+
83
+ func numberOfSections(in collectionView: UICollectionView) -> Int {
84
+
85
+ return 2
86
+
87
+ }
88
+
89
+ func collectionView(_ collectionView : UICollectionView, numberOfItemsInSection section: Int) -> Int {
90
+
91
+ if section == 0 {
92
+
93
+ print("generated (interests.count) d-cells")
94
+
95
+ return interests.count
96
+
97
+ } else{
98
+
99
+ print("generated (subCommittees.count) s-cells")
100
+
101
+ return subCommittees.count
102
+
103
+ }
104
+
105
+ }
106
+
107
+
108
+
65
- collectionView.register(CollectionViewCell_D.self, forCellWithReuseIdentifier: "CollectionViewCell_D")
109
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
110
+
66
-
111
+ if indexPath.section == 0 {
112
+
113
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell_D", for: indexPath) as! CollectionViewCell_D
114
+
115
+ let interest = interests[indexPath.item]
116
+
117
+ cell.interest = interest
118
+
119
+ cell.backgroundColor = UIColor.yellow
120
+
121
+ print("add items to (indexPath.item)")
122
+
123
+ return cell
124
+
125
+ }else{
126
+
67
- collectionView.register(CollectionViewCell_S.self, forCellWithReuseIdentifier: "CollectionViewCell_S")
127
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell_S", for: indexPath) as! CollectionViewCell_S
68
-
128
+
69
- let layout = UICollectionViewFlowLayout()
129
+ let subCommittee = subCommittees[indexPath.item]
70
-
130
+
71
- layout.scrollDirection = .vertical
131
+ cell.subCommittee = subCommittee
132
+
133
+ cell.backgroundColor = UIColor.green
134
+
135
+ print("add items to (indexPath.item)")
136
+
137
+ return cell
138
+
139
+ }
140
+
141
+ }
142
+
143
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
72
144
 
73
145
 
74
146
 
75
- }
76
-
77
-
78
-
79
- func numberOfSections(in collectionView: UICollectionView) -> Int {
80
-
81
- return 2
82
-
83
- }
84
-
85
- func collectionView(_ collectionView : UICollectionView, numberOfItemsInSection section: Int) -> Int {
86
-
87
- if section == 0 {
88
-
89
- print("generated (interests.count) d-cells")
90
-
91
- return interests.count
92
-
93
- } else{
94
-
95
- print("generated (subCommittees.count) s-cells")
96
-
97
- return subCommittees.count
98
-
99
- }
100
-
101
- }
102
-
103
-
104
-
105
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
106
-
107
147
  if indexPath.section == 0 {
108
148
 
109
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell_D", for: indexPath) as! CollectionViewCell_D
110
-
111
- let interest = interests[indexPath.item]
149
+ let cellSize = self.view.bounds.width / 4
112
-
113
- cell.interest = interest
150
+
114
-
115
- print("add items to (indexPath.item)")
151
+ print("width: (cellSize)"+"height: 150")
116
-
152
+
117
- return cell
153
+ return CGSize(width: cellSize, height: 150)
118
154
 
119
155
  }else{
120
156
 
121
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell_S", for: indexPath) as! CollectionViewCell_S
122
-
123
- let subCommittee = subCommittees[indexPath.item]
124
-
125
- cell.subCommittee = subCommittee
126
-
127
- print("add items to (indexPath.item)")
128
-
129
- return cell
130
-
131
- }
132
-
133
- }
134
-
135
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
157
+ let cellSize : CGFloat = self.view.bounds.width - 16
158
+
159
+ print("width:(cellSize)"+"height: 150")
160
+
161
+ return CGSize(width: cellSize, height: 250)
162
+
163
+ }
164
+
165
+ }
166
+
167
+ func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
168
+
169
+
170
+
171
+ guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SectionHeader", for: indexPath) as? SectionHeader else {
172
+
173
+ fatalError("Could not find proper header")
174
+
175
+ }
176
+
177
+
178
+
179
+ if kind == UICollectionView.elementKindSectionHeader {
180
+
181
+ if indexPath.section == 0 {
182
+
183
+ header.sectionLabel?.text = "討論会"
184
+
185
+ }else{
186
+
187
+ header.sectionLabel?.text = "分科会"
188
+
189
+ }
190
+
191
+ return header
192
+
193
+ }
194
+
195
+
196
+
197
+ return UICollectionReusableView()
198
+
199
+ }
200
+
201
+
202
+
203
+ }
204
+
205
+ ```
206
+
207
+ ↓1つのcollectionViewに`CollectionViewCell_D`と`CollectionViewCell_S`を登録して使っておりますが同内容であるため文字数の都合上`CollectionViewCell_D`のみ掲載します
208
+
209
+ ```swift
210
+
211
+ import UIKit
212
+
213
+
214
+
215
+ class CollectionViewCell_D: UICollectionViewCell {
216
+
217
+
218
+
219
+ @IBOutlet weak var view_D: UIView!
220
+
221
+ @IBOutlet weak var imageView_D: UIImageView!
222
+
223
+ @IBOutlet weak var titile_D: UILabel!
224
+
225
+
226
+
227
+ var interest: Interest!{
228
+
229
+ didSet{
230
+
231
+ print(title_D)
232
+
233
+        // nilが返る
234
+
235
+ self.updateUI()
236
+
237
+ }
238
+
239
+ }
240
+
241
+ func updateUI() {
242
+
243
+ if let interest = interest {
244
+
245
+ imageView_D.image = interest.featuredImage
246
+
247
+ titile_D.text = interest.title
248
+
249
+ view_D.backgroundColor = interest.color
250
+
251
+ }else{
252
+
253
+ imageView_D.image = nil
254
+
255
+ titile_D.text = nil
256
+
257
+ view_D.backgroundColor = nil
258
+
259
+ }
260
+
261
+ view_D.layer.cornerRadius = 5.0
262
+
263
+ view_D.layer.masksToBounds = true
264
+
265
+ imageView_D.layer.cornerRadius = 5.0
266
+
267
+ imageView_D.layer.masksToBounds = true
136
268
 
137
269
 
138
270
 
139
- if indexPath.section == 0 {
140
-
141
- let cellSize = self.view.bounds.width / 4
142
-
143
- print("width: (cellSize)"+"height: 150")
144
-
145
- return CGSize(width: cellSize, height: 150)
146
-
147
- }else{
148
-
149
- let cellSize : CGFloat = self.view.bounds.width - 16
150
-
151
- print("width:(cellSize)"+"height: 150")
152
-
153
- return CGSize(width: cellSize, height: 250)
154
-
155
- }
156
-
157
- }
158
-
159
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
160
-
161
-
162
-
163
- guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SectionHeader", for: indexPath) as? SectionHeader else {
164
-
165
- fatalError("Could not find proper header")
166
-
167
- }
168
-
169
-
170
-
171
- if kind == UICollectionView.elementKindSectionHeader {
172
-
173
- if indexPath.section == 0 {
174
-
175
- header.sectionLabel?.text = "討論会"
176
-
177
- }else{
178
-
179
- header.sectionLabel?.text = "分科会"
180
-
181
- }
182
-
183
- return header
184
-
185
- }
186
-
187
-
188
-
189
- return UICollectionReusableView()
190
-
191
- }
192
-
193
-
271
+ }
194
272
 
195
273
  }
196
274
 
275
+
276
+
197
- ```
277
+ ```
198
-
278
+
199
- 1つのcollectionViewに`CollectionViewCell_D`と`CollectionViewCell_S`を登録して使っておりますが同内容であるため文字数の都合上`CollectionViewCell_D`のみ掲載ます
279
+ データを扱うクラスは`Interest`と`Subcommittee`がありますが同内容であるため`Interest`のみ掲載させていただきます
200
-
280
+
201
- ```swift
281
+ ```
202
282
 
203
283
  import UIKit
204
284
 
205
285
 
206
286
 
207
- class CollectionViewCell_D: UICollectionViewCell {
208
-
209
-
210
-
211
- @IBOutlet weak var view_D: UIView!
212
-
213
- @IBOutlet weak var imageView_D: UIImageView!
214
-
215
- @IBOutlet weak var titile_D: UILabel!
216
-
217
-
218
-
219
- var interest: Interest!{
287
+ class Interest {
220
-
288
+
289
+
290
+
221
- didSet{
291
+ var title = ""
292
+
222
-
293
+ var featuredImage: UIImage
294
+
295
+ var color: UIColor
296
+
297
+
298
+
299
+ init(title: String, featuredImage: UIImage, color: UIColor)
300
+
301
+ {
302
+
223
- self.updateUI()
303
+ self.title = title
304
+
224
-
305
+ self.featuredImage = featuredImage
306
+
307
+ self.color = color
308
+
225
- }
309
+ }
310
+
226
-
311
+ static func fetchInterest() -> [Interest]
312
+
313
+ {
314
+
315
+ return[
316
+
317
+ Interest(title: "How can we do to stop the war?", featuredImage: UIImage(named: "war")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
318
+
319
+ Interest(title: "Should we ban zoo?", featuredImage: UIImage(named: "animals")!, color: UIColor(red: 20/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
320
+
321
+ Interest(title: "Should we ban wearing the veil?", featuredImage: UIImage(named: "international")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
322
+
323
+ Interest(title: "Should we legalize all of th gambling?", featuredImage: UIImage(named: "politics")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
324
+
325
+ Interest(title: "How we reduce green house gas?", featuredImage: UIImage(named: "environment")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6))
326
+
327
+ ]
328
+
227
- }
329
+ }
228
-
229
- func updateUI() {
330
+
230
-
231
- if let interest = interest {
331
+
232
-
233
- imageView_D.image = interest.featuredImage
332
+
234
-
235
- titile_D.text = interest.title
333
+
236
-
237
- view_D.backgroundColor = interest.color
238
-
239
- }else{
240
-
241
- imageView_D.image = nil
242
-
243
- titile_D.text = nil
244
-
245
- view_D.backgroundColor = nil
246
-
247
- }
248
-
249
- view_D.layer.cornerRadius = 5.0
250
-
251
- view_D.layer.masksToBounds = true
252
-
253
- imageView_D.layer.cornerRadius = 5.0
254
-
255
- imageView_D.layer.masksToBounds = true
256
-
257
-
258
-
259
- }
260
334
 
261
335
  }
262
336
 
263
-
264
-
265
- ```
266
-
267
- ↓データを扱うクラスは`Interest`と`Subcommittee`がありますが同内容であるため`Interest`のみ掲載させていただきます
268
-
269
- ```
270
-
271
- import UIKit
272
-
273
-
274
-
275
- class Interest {
276
-
277
-
278
-
279
- var title = ""
280
-
281
- var featuredImage: UIImage
282
-
283
- var color: UIColor
284
-
285
-
286
-
287
- init(title: String, featuredImage: UIImage, color: UIColor)
288
-
289
- {
290
-
291
- self.title = title
292
-
293
- self.featuredImage = featuredImage
294
-
295
- self.color = color
296
-
297
- }
298
-
299
- static func fetchInterest() -> [Interest]
300
-
301
- {
302
-
303
- return[
304
-
305
- Interest(title: "How can we do to stop the war?", featuredImage: UIImage(named: "war")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
306
-
307
- Interest(title: "Should we ban zoo?", featuredImage: UIImage(named: "animals")!, color: UIColor(red: 20/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
308
-
309
- Interest(title: "Should we ban wearing the veil?", featuredImage: UIImage(named: "international")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
310
-
311
- Interest(title: "Should we legalize all of th gambling?", featuredImage: UIImage(named: "politics")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6)),
312
-
313
- Interest(title: "How we reduce green house gas?", featuredImage: UIImage(named: "environment")!, color: UIColor(red: 178/255.0, green: 98/255.0, blue: 71/255.0, alpha: 0.6))
314
-
315
- ]
316
-
317
- }
318
-
319
-
320
-
321
-
322
-
323
- }
324
-
325
337
  ```
326
338
 
327
339
  ### 自分が考えて実行したこと
@@ -342,9 +354,9 @@
342
354
 
343
355
  エラーが出たところをにオプショナルチェイニングをしました。すると
344
356
 
357
+ ![イメージ説明](ff2d8714b748f5637dd2b05254ccb2e9.png)
358
+
345
- ![イメージ説明](673c0cd0ec3f87e6cf1dd5ec793df503.png)
359
+ ![イメージ説明](5591f9338bf04cd2f6fa4dbe46153eea.png)
346
-
347
- ![ヒエラルキー](64bcecc42875e41edd61ea0d9482b4d7.png)
348
360
 
349
361
  提示させていただいたコードにあるprint文の出力は正常になされており、cellの数、大きさなどに異常はありませんがcellの部品は表示されていません
350
362