質問編集履歴

1

指摘された処理の実装

2020/09/08 01:00

投稿

atk_721
atk_721

スコア62

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,11 @@
44
44
 
45
45
 
46
46
 
47
- private let tableCell = "MainTableCell"
47
+ public let tableCell = "MainTableCell"
48
+
49
+ public let collectionCell = "CollectionCell"
50
+
51
+ private let sectionReuseIdentifier = "SectionHeader"
48
52
 
49
53
 
50
54
 
@@ -56,17 +60,21 @@
56
60
 
57
61
 
58
62
 
63
+ private var scrollUpDirection: Bool = false
64
+
65
+ private let collectionSectionInSets: UIEdgeInsets = .init(top: 0, left: 10, bottom: 0, right: 10)
66
+
59
67
  private var prevcontentOffSet: CGPoint = .init(x: 0, y: 0)
60
68
 
61
69
  private let headerAnimationSpeed: CGFloat = 12
62
70
 
63
- private let headerHeight: CGFloat = 180
71
+ private let headerHeight: CGFloat = 120
64
72
 
65
73
  private var headerViewHeightConstraint: NSLayoutConstraint!
66
74
 
67
75
  private lazy var screenSize = self.view.bounds
68
76
 
69
- private let tableView = UITableView(frame: .zero, style: .grouped)
77
+ private let tableView = UITableView(frame: .zero, style: .plain)
70
78
 
71
79
  private let headerView:MainHeaderView = {
72
80
 
@@ -100,9 +108,19 @@
100
108
 
101
109
 
102
110
 
103
-
111
+ override var prefersStatusBarHidden: Bool {
104
-
112
+
105
- //MARK: - Selectors
113
+ return scrollUpDirection
114
+
115
+ }
116
+
117
+
118
+
119
+ override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
120
+
121
+ return .slide
122
+
123
+ }
106
124
 
107
125
 
108
126
 
@@ -148,9 +166,17 @@
148
166
 
149
167
  tableView.dataSource = self
150
168
 
151
- tableView.register(MainTableViewCell.self, forCellReuseIdentifier: tableCell)
169
+ tableView.register(UITableViewCell.self, forCellReuseIdentifier: tableCell)
152
-
170
+
153
- tableView.rowHeight = 80
171
+ tableView.rowHeight = 200
172
+
173
+ tableView.tableFooterView = UIView()
174
+
175
+ tableView.register(MainTableCell.self, forCellReuseIdentifier: tableCell)
176
+
177
+ tableView.register(SectionHeaderView.self, forHeaderFooterViewReuseIdentifier: sectionReuseIdentifier)
178
+
179
+ tableView.contentInsetAdjustmentBehavior = .never
154
180
 
155
181
  view.addSubview(tableView)
156
182
 
@@ -184,6 +210,20 @@
184
210
 
185
211
  }
186
212
 
213
+
214
+
215
+ fileprivate func animationStatusBar() {
216
+
217
+
218
+
219
+ UIView.animate(withDuration: 0.3) {
220
+
221
+ self.setNeedsStatusBarAppearanceUpdate()
222
+
223
+ }
224
+
225
+ }
226
+
187
227
  }
188
228
 
189
229
 
@@ -200,17 +240,17 @@
200
240
 
201
241
 
202
242
 
243
+ tableView.dequeueReusableHeaderFooterView(withIdentifier: sectionReuseIdentifier)
244
+
245
+ let sectionHeader = SectionHeaderView(reuseIdentifier: sectionReuseIdentifier)
246
+
203
- return SectionHeaderView(prefeturesName: Prefectures[section])
247
+ sectionHeader.prefecturesName = Prefectures[section]
248
+
204
-
249
+ return sectionHeader
250
+
205
- }
251
+ }
206
-
207
- func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
252
+
208
-
209
-
210
-
211
- return Prefectures[section]
253
+
212
-
213
- }
214
254
 
215
255
  func numberOfSections(in tableView: UITableView) -> Int {
216
256
 
@@ -220,7 +260,17 @@
220
260
 
221
261
  }
222
262
 
223
-
263
+ func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
264
+
265
+
266
+
267
+ guard let cell = cell as? MainTableCell else { return }
268
+
269
+
270
+
271
+ cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forRow: indexPath.section)
272
+
273
+ }
224
274
 
225
275
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
226
276
 
@@ -236,111 +286,235 @@
236
286
 
237
287
 
238
288
 
239
- let cell = tableView.dequeueReusableCell(withIdentifier: tableCell, for: indexPath) as! MainTableViewCell
289
+ let cell = tableView.dequeueReusableCell(withIdentifier: tableCell, for: indexPath)
240
-
241
- cell.textLabel?.text = "hello"
290
+
291
+
242
292
 
243
293
  return cell
244
294
 
245
295
  }
246
296
 
297
+
298
+
299
+ func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
300
+
301
+
302
+
303
+ tableView.deselectRow(at: indexPath, animated: true)
304
+
305
+ }
306
+
247
307
  }
248
308
 
249
309
 
250
310
 
251
- //MARK: - ScrollView Delegate
252
-
253
-
254
-
255
- extension MainController {
256
-
257
-
258
-
259
- func scrollViewDidScroll(_ scrollView: UIScrollView) {
260
-
261
-
262
-
263
- let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
264
-
265
-
266
-
267
- UIView.animate(withDuration: 0.4) {
268
-
269
- if translation.y > 0 {
270
-
271
- if self.headerViewHeightConstraint.constant > self.headerHeight { return }
272
-
273
- self.headerViewHeightConstraint.constant += self.headerAnimationSpeed
274
-
275
- self.view.layoutIfNeeded()
276
-
277
- } else {
278
-
279
- if self.headerViewHeightConstraint.constant < 0 { return }
280
-
281
- self.headerViewHeightConstraint.constant -= self.headerAnimationSpeed
282
-
283
- self.view.layoutIfNeeded()
284
-
285
- }
311
+ //MARK: CollectionView Delegate DataSource
312
+
313
+
314
+
315
+ extension MainController: UICollectionViewDelegate, UICollectionViewDataSource {
316
+
317
+
318
+
319
+ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
320
+
321
+
322
+
323
+ return 10
324
+
325
+ }
326
+
327
+
328
+
329
+ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
330
+
331
+
332
+
333
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionCell, for: indexPath) as! MainCollectionCell
334
+
335
+ return cell
336
+
337
+ }
338
+
339
+ }
340
+
341
+
342
+
343
+ //MARK: - CollectionView Flowlayout
344
+
345
+
346
+
347
+ extension MainController: UICollectionViewDelegateFlowLayout {
348
+
349
+
350
+
351
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
352
+
353
+
354
+
355
+ return CGSize(width: 200, height: 180)
356
+
357
+ }
358
+
359
+
360
+
361
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
362
+
363
+
364
+
365
+ return collectionSectionInSets
366
+
367
+ }
368
+
369
+
370
+
371
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
372
+
373
+
374
+
375
+ return collectionSectionInSets.left
376
+
377
+ }
378
+
379
+
380
+
381
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
382
+
383
+
384
+
385
+ return collectionSectionInSets.left
386
+
387
+ }
388
+
389
+
390
+
391
+ }
392
+
393
+
394
+
395
+
396
+
397
+
398
+
399
+
400
+
401
+ ```
402
+
403
+ ##SectionHeaderView
404
+
405
+ ```ここに言語を入力
406
+
407
+ import UIKit
408
+
409
+
410
+
411
+ class SectionHeaderView: UITableViewHeaderFooterView {
412
+
413
+
414
+
415
+ //MARK: - Properties
416
+
417
+
418
+
419
+ internal var prefecturesName: String? {
420
+
421
+ didSet {
422
+
423
+ guard let prefecturesName = prefecturesName else { return }
424
+
425
+ titleLabel.text = prefecturesName
286
426
 
287
427
  }
288
428
 
289
429
  }
290
430
 
291
-
292
-
293
- func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
294
-
295
- insertHeaderWhenEndAnimation()
296
-
297
- }
298
-
299
-
300
-
301
- func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
302
-
303
- insertHeaderWhenEndAnimation()
304
-
305
- }
306
-
307
-
308
-
309
- fileprivate func insertHeaderWhenEndAnimation() {
310
-
311
-
312
-
313
- if self.headerViewHeightConstraint.constant > self.headerHeight { return }
314
-
315
- if self.headerViewHeightConstraint.constant < 0 { return }
316
-
317
- if headerViewHeightConstraint.constant < headerHeight/2 {
318
-
319
-
320
-
321
- UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.85, initialSpringVelocity: 0.7, options: [], animations: {
322
-
323
-
324
-
325
- self.headerViewHeightConstraint.constant = 0
326
-
327
- self.view.layoutIfNeeded()
328
-
329
- })
330
-
331
- }else{
332
-
333
- UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.85, initialSpringVelocity: 0.7, options: [], animations: {
334
-
335
-
336
-
337
- self.headerViewHeightConstraint.constant = self.headerHeight
338
-
339
- self.view.layoutIfNeeded()
340
-
341
- })
342
-
343
- }
431
+ private lazy var titleLabel: UILabel = {
432
+
433
+
434
+
435
+ let label = UILabel()
436
+
437
+
438
+
439
+ label.font = UIFont.boldSystemFont(ofSize: 24)
440
+
441
+ label.textAlignment = .center
442
+
443
+ label.textColor = .black
444
+
445
+
446
+
447
+ return label
448
+
449
+ }()
450
+
451
+
452
+
453
+ private let showButton: UIButton = {
454
+
455
+
456
+
457
+ let button = UIButton(type: .system)
458
+
459
+
460
+
461
+ button.setTitle("全て見る", for: .normal)
462
+
463
+ button.titleLabel?.textColor = .systemBlue
464
+
465
+ return button
466
+
467
+ }()
468
+
469
+
470
+
471
+ //MARK: - View LifeCycle
472
+
473
+
474
+
475
+ override init(reuseIdentifier: String?) {
476
+
477
+ super.init(reuseIdentifier: reuseIdentifier)
478
+
479
+
480
+
481
+ setHeight(height: 60)
482
+
483
+ }
484
+
485
+
486
+
487
+ override func layoutSubviews() {
488
+
489
+
490
+
491
+ let view = UIView()
492
+
493
+ view.frame = self.frame
494
+
495
+ view.backgroundColor = .white
496
+
497
+ backgroundView = view
498
+
499
+ self.addSubview(titleLabel)
500
+
501
+ titleLabel.centerY(inView: self)
502
+
503
+ titleLabel.anchor(left: leftAnchor, padddingLeft: 16)
504
+
505
+
506
+
507
+ addSubview(showButton)
508
+
509
+ showButton.centerY(inView: self)
510
+
511
+ showButton.anchor(right: rightAnchor, paddingRight: -16)
512
+
513
+ }
514
+
515
+ required init?(coder: NSCoder) {
516
+
517
+ fatalError("init(coder:) has not been implemented")
344
518
 
345
519
  }
346
520
 
@@ -350,151 +524,7 @@
350
524
 
351
525
  ```
352
526
 
353
- ##SectionHeaderView
527
+
354
-
355
- ```ここに言語を入力
356
-
357
-
358
-
359
- import UIKit
360
-
361
-
362
-
363
- class SectionHeaderView: UIView {
364
-
365
-
366
-
367
- //MARK: - Properties
368
-
369
-
370
-
371
- private let prefecturesName: String!
372
-
373
- private lazy var titleLabel: UILabel = {
374
-
375
-
376
-
377
- let label = UILabel()
378
-
379
-
380
-
381
- label.text = prefecturesName
382
-
383
- label.font = UIFont.boldSystemFont(ofSize: 24)
384
-
385
- label.textAlignment = .center
386
-
387
- label.textColor = .black
388
-
389
-
390
-
391
- return label
392
-
393
- }()
394
-
395
-
396
-
397
- //MARK: - View LifeCycle
398
-
399
-
400
-
401
- init(prefeturesName: String) {
402
-
403
- self.prefecturesName = prefeturesName
404
-
405
- super.init(frame: .zero)
406
-
407
-
408
-
409
- setHeight(height: 40)
410
-
411
-
412
-
413
- self.addSubview(titleLabel)
414
-
415
- titleLabel.centerX(inView: self)
416
-
417
- titleLabel.centerY(inView: self)
418
-
419
- }
420
-
421
-
422
-
423
- required init?(coder: NSCoder) {
424
-
425
- fatalError("init(coder:) has not been implemented")
426
-
427
- }
428
-
429
-
430
-
431
- //MARK: - Helpers
432
-
433
- }
434
-
435
-
436
-
437
- ```
438
-
439
- ## MainHeaderView
440
-
441
- ```ここに言語を入力
442
-
443
- import UIKit
444
-
445
-
446
-
447
- class MainHeaderView: UIView {
448
-
449
-
450
-
451
-
452
-
453
- private let titleLabel: UILabel = {
454
-
455
-
456
-
457
- let label = UILabel()
458
-
459
-
460
-
461
- label.text = "hello"
462
-
463
-
464
-
465
- return label
466
-
467
- }()
468
-
469
-
470
-
471
- override init(frame: CGRect) {
472
-
473
- super.init(frame: frame)
474
-
475
-
476
-
477
- self.addSubview(titleLabel)
478
-
479
- titleLabel.centerX(inView: self)
480
-
481
- titleLabel.centerY(inView: self)
482
-
483
-
484
-
485
- }
486
-
487
-
488
-
489
- required init?(coder: NSCoder) {
490
-
491
- fatalError("init(coder:) has not been implemented")
492
-
493
- }
494
-
495
- }
496
-
497
- ```
498
528
 
499
529
  ##起動時の画面
500
530
 
@@ -510,6 +540,12 @@
510
540
 
511
541
  ### 補足情報(FW/ツールのバージョンなど)
512
542
 
513
-
514
-
515
543
  swift5 xcode 11.6
544
+
545
+
546
+
547
+ ##追記
548
+
549
+ 1000文字を超えてしまうので不要なコードを添削しました。
550
+
551
+ sectionHeaderViewを登録して使いまわす様に変更しました。