質問編集履歴

1

ソースコードを追加しました

2021/08/10 06:12

投稿

yuka0128
yuka0128

スコア4

test CHANGED
File without changes
test CHANGED
@@ -28,7 +28,487 @@
28
28
 
29
29
 
30
30
 
31
-
31
+ ソースコードを添付しますがprotocolを使っているので見にくいかもしれません。申し訳ありません。
32
+
33
+
34
+
35
+
36
+
37
+ controller側のソースコード
38
+
39
+ ```Swift
40
+
41
+ import UIKit
42
+
43
+
44
+
45
+ private let reuseIdentifier = "UserCell"
46
+
47
+ private let headerIdentifier = "HeaderView"
48
+
49
+
50
+
51
+ class BlockedListController: UICollectionViewController {
52
+
53
+
54
+
55
+ //MARK: - Properties
56
+
57
+
58
+
59
+ private var users = [User]() {
60
+
61
+ didSet { collectionView.reloadData() }
62
+
63
+ }
64
+
65
+
66
+
67
+ private var ifNoOneLabel: UILabel = {
68
+
69
+ let label = UILabel()
70
+
71
+ label.isHidden = true
72
+
73
+ label.text = "現在ブロック中のアカウントはいません????"
74
+
75
+ label.textAlignment = .center
76
+
77
+ label.textColor = .white
78
+
79
+ label.font = UIFont.boldSystemFont(ofSize: 14)
80
+
81
+ return label
82
+
83
+ }()
84
+
85
+
86
+
87
+ private var ifNoOneLabel2: UILabel = {
88
+
89
+ let label = UILabel()
90
+
91
+ label.isHidden = true
92
+
93
+ label.text = "ブロックしたアカウントはここに表示されます"
94
+
95
+ label.textAlignment = .center
96
+
97
+ label.textColor = .white
98
+
99
+ label.font = UIFont.systemFont(ofSize: 12)
100
+
101
+ return label
102
+
103
+ }()
104
+
105
+
106
+
107
+ //MARK: - Lifecycle
108
+
109
+
110
+
111
+ override func viewDidLoad() {
112
+
113
+ super.viewDidLoad()
114
+
115
+ configureUI()
116
+
117
+ blockedUsersAreExist()
118
+
119
+ fetchBlockedUsers()
120
+
121
+ }
122
+
123
+
124
+
125
+ override func viewDidLayoutSubviews() {
126
+
127
+ super.viewDidLayoutSubviews()
128
+
129
+ fetchBlockedUsers()
130
+
131
+ }
132
+
133
+
134
+
135
+ //MARK: - API
136
+
137
+
138
+
139
+ private func fetchBlockedUsers() {
140
+
141
+ UserService.fetchBlockedUsers { users in
142
+
143
+ self.users = users
144
+
145
+ }
146
+
147
+ }
148
+
149
+
150
+
151
+ private func blockedUsersAreExist() {
152
+
153
+ UserService.blockedUsersAreExist { bool in
154
+
155
+ self.ifNoOneLabel.isHidden = !bool
156
+
157
+ self.ifNoOneLabel2.isHidden = !bool
158
+
159
+ }
160
+
161
+ }
162
+
163
+
164
+
165
+ //MARK: - Helpers
166
+
167
+
168
+
169
+ private func configureUI() {
170
+
171
+ collectionView.backgroundColor = .backgroundColor
172
+
173
+ collectionView.register(BlockedListCell.self, forCellWithReuseIdentifier: reuseIdentifier)
174
+
175
+ collectionView.register(BlockedListHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier)
176
+
177
+
178
+
179
+ let stack = UIStackView(arrangedSubviews: [ifNoOneLabel, ifNoOneLabel2])
180
+
181
+ stack.axis = .vertical
182
+
183
+ stack.spacing = 4
184
+
185
+
186
+
187
+ collectionView.addSubview(stack)
188
+
189
+ stack.centerX(inView: collectionView)
190
+
191
+ stack.anchor(top: collectionView.topAnchor, paddingTop: 240)
192
+
193
+ }
194
+
195
+ }
196
+
197
+
198
+
199
+ //MARK: - UICollectionViewDataSource
200
+
201
+
202
+
203
+ extension BlockedListController {
204
+
205
+ override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
206
+
207
+ return users.count
208
+
209
+ }
210
+
211
+
212
+
213
+ override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
214
+
215
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! BlockedListCell
216
+
217
+ cell.delegate = self
218
+
219
+ cell.viewModel = UserCellViewModel(user: users[indexPath.row])
220
+
221
+ return cell
222
+
223
+ }
224
+
225
+ }
226
+
227
+
228
+
229
+ //MARK: - UICollectionViewDelegate
230
+
231
+
232
+
233
+ extension BlockedListController {
234
+
235
+ override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
236
+
237
+ let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! BlockedListHeader
238
+
239
+ header.delegate = self
240
+
241
+ return header
242
+
243
+ }
244
+
245
+ }
246
+
247
+
248
+
249
+ //MARK: - UICollectionViewDelegateFlowLayout
250
+
251
+
252
+
253
+ extension BlockedListController: UICollectionViewDelegateFlowLayout {
254
+
255
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
256
+
257
+ return 1
258
+
259
+ }
260
+
261
+
262
+
263
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
264
+
265
+ return 1
266
+
267
+ }
268
+
269
+
270
+
271
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
272
+
273
+ let width = (view.frame.width - 2) / 3
274
+
275
+ return CGSize(width: width, height: width)
276
+
277
+ }
278
+
279
+
280
+
281
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
282
+
283
+ return CGSize(width: view.frame.width, height: 60)
284
+
285
+ }
286
+
287
+ }
288
+
289
+
290
+
291
+ //MARK: - BlockedListHeaderDelegate
292
+
293
+
294
+
295
+ extension BlockedListController: BlockedListHeaderDelegate {
296
+
297
+ func handleDismissal() {
298
+
299
+ navigationController?.popViewController(animated: true)
300
+
301
+ }
302
+
303
+ }
304
+
305
+
306
+
307
+ //MARK: - BlockedListCellDelegate
308
+
309
+
310
+
311
+ extension BlockedListController: BlockedListCellDelegate {
312
+
313
+ func unblockUser(withUid uid: String) {
314
+
315
+ UserService.unblockUser(withUid: uid) { _ in
316
+
317
+ self.fetchBlockedUsers()
318
+
319
+ self.collectionView.reloadData()
320
+
321
+ }
322
+
323
+ }
324
+
325
+ }
326
+
327
+ ```
328
+
329
+
330
+
331
+
332
+
333
+ cell側のソースコード
334
+
335
+ ```Swift
336
+
337
+ import UIKit
338
+
339
+ import SDWebImage
340
+
341
+
342
+
343
+ protocol BlockedListCellDelegate: AnyObject {
344
+
345
+ func unblockUser(withUid uid: String)
346
+
347
+ }
348
+
349
+
350
+
351
+ class BlockedListCell: UICollectionViewCell {
352
+
353
+
354
+
355
+ //MARK: - Properties
356
+
357
+
358
+
359
+ weak var delegate: BlockedListCellDelegate?
360
+
361
+
362
+
363
+ var viewModel: UserCellViewModel? {
364
+
365
+ didSet { populateUserData() }
366
+
367
+ }
368
+
369
+
370
+
371
+ private let profileImageView: UIImageView = {
372
+
373
+ let iv = UIImageView()
374
+
375
+ iv.contentMode = .scaleAspectFill
376
+
377
+ iv.backgroundColor = .lightGray
378
+
379
+ iv.clipsToBounds = true
380
+
381
+ return iv
382
+
383
+ }()
384
+
385
+
386
+
387
+ private let fullnameLabel: UILabel = {
388
+
389
+ let label = UILabel()
390
+
391
+ label.textColor = .white
392
+
393
+ label.font = UIFont.boldSystemFont(ofSize: 14)
394
+
395
+ return label
396
+
397
+ }()
398
+
399
+
400
+
401
+ private lazy var unblockButton: UIButton = {
402
+
403
+ let button = UIButton(type: .system)
404
+
405
+ button.setImage(UIImage(systemName: "xmark.circle.fill"), for: .normal)
406
+
407
+ button.setDimensions(height: 30, width: 30)
408
+
409
+ button.tintColor = .lightGray
410
+
411
+ button.addTarget(self, action: #selector(handleUnblock), for: .touchUpInside)
412
+
413
+ return button
414
+
415
+ }()
416
+
417
+
418
+
419
+ //MARK: - Lifecycle
420
+
421
+
422
+
423
+ override init(frame: CGRect) {
424
+
425
+ super.init(frame: frame)
426
+
427
+ configureUI()
428
+
429
+ }
430
+
431
+
432
+
433
+ required init?(coder: NSCoder) {
434
+
435
+ fatalError("init(coder:) has not been implemented")
436
+
437
+ }
438
+
439
+
440
+
441
+ //MARK: - Actions
442
+
443
+
444
+
445
+ @objc func handleUnblock() {
446
+
447
+ guard let uid = viewModel?.user.uid else { return }
448
+
449
+ delegate?.unblockUser(withUid: uid)
450
+
451
+ }
452
+
453
+
454
+
455
+ //MARK: - Helpers
456
+
457
+
458
+
459
+ private func configureUI() {
460
+
461
+ addSubview(profileImageView)
462
+
463
+ profileImageView.setDimensions(height: 80, width: 80)
464
+
465
+ profileImageView.layer.cornerRadius = 80 / 2
466
+
467
+ profileImageView.centerX(inView: self)
468
+
469
+ profileImageView.centerY(inView: self)
470
+
471
+
472
+
473
+ addSubview(fullnameLabel)
474
+
475
+ fullnameLabel.centerX(inView: profileImageView)
476
+
477
+ fullnameLabel.anchor(top: profileImageView.bottomAnchor, paddingTop: 8)
478
+
479
+
480
+
481
+ addSubview(unblockButton)
482
+
483
+ unblockButton.anchor(top: topAnchor, right: rightAnchor, paddingTop: 6, paddingRight: 6)
484
+
485
+ }
486
+
487
+
488
+
489
+ private func populateUserData() {
490
+
491
+ guard let viewModel = viewModel else { return }
492
+
493
+ fullnameLabel.text = viewModel.fullname
494
+
495
+ profileImageView.sd_setImage(with: viewModel.profileImageUrl, completed: nil)
496
+
497
+ }
498
+
499
+ }
500
+
501
+
502
+
503
+
504
+
505
+ ```
506
+
507
+
508
+
509
+
510
+
511
+ ビルドするとこんな感じです。
32
512
 
33
513
  このイメージ右上の✖︎を押下したらそのcellを削除したい
34
514