質問編集履歴

5

追加

2019/02/19 14:32

投稿

amazon_106
amazon_106

スコア50

test CHANGED
File without changes
test CHANGED
@@ -574,4 +574,4 @@
574
574
 
575
575
 
576
576
 
577
- この状態でfollowボタンを押しても、エラーもコンソール上にも反応がありませんでした。
577
+ この状態でfollowボタンを押しても、Thread 1: breakpoint 2.1もコンソール上にも反応がありませんでした。

4

デバック

2019/02/19 14:32

投稿

amazon_106
amazon_106

スコア50

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,3 @@
1
- タイトルのことをしたかったのですが、その前のテストで詰まっています。
2
-
3
1
  ##現在詰まっているところ
4
2
 
5
3
  followボタンをクリックし```print("タップ")```を出力したい
@@ -572,8 +570,8 @@
572
570
 
573
571
 
574
572
 
573
+ ![イメージ説明](75e38ff54ee830de81d7421b82fd29e6.png)
574
+
575
+
576
+
575
- でコンソールに「タップ」と出力されて欲しいのです、出ません。
577
+ の状態followボタンを押しても、エラーもコンソールも反応ありませんでした
576
-
577
- 原因に検討がつく方や似たような実装をした方、もしいらっしゃいましたら、お力添え頂きたいです。
578
-
579
- よろしくお願いします。

3

追加

2019/02/19 14:31

投稿

amazon_106
amazon_106

スコア50

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,89 @@
14
14
 
15
15
 
16
16
 
17
+ import UIKit
18
+
19
+ import Firebase
20
+
21
+
22
+
23
+ class UserProfileHeader: UICollectionViewCell {
24
+
25
+
26
+
27
+ // MARK: - Properties
28
+
17
- var delegate: UserProfileHeaderDelegate?
29
+ var delegate: UserProfileHeaderDelegate?
30
+
31
+
32
+
18
-
33
+ var user: User? {
34
+
35
+
36
+
19
-
37
+ didSet {
38
+
39
+
40
+
20
-
41
+ // configure edit profile button
42
+
43
+ configureEditProfileFollowButton()
44
+
45
+
46
+
47
+ // set user stats
48
+
49
+ setUserStats(for: user)
50
+
51
+
52
+
53
+ let fullName = user?.name
54
+
55
+ nameLabel.text = fullName
56
+
57
+
58
+
59
+ profileImageView.loadImage(with: (user?.profileImageUrl)!)
60
+
61
+ }
62
+
63
+ }
64
+
65
+
66
+
67
+ let profileImageView: UIImageView = {
68
+
69
+ let iv = UIImageView()
70
+
71
+ iv.contentMode = .scaleAspectFill
72
+
73
+ iv.clipsToBounds = true
74
+
75
+ iv.backgroundColor = .red
76
+
77
+ return iv
78
+
79
+ }()
80
+
81
+
82
+
83
+ let nameLabel: UILabel = {
84
+
85
+ let label = UILabel()
86
+
87
+ label.font = UIFont.boldSystemFont(ofSize: 12)
88
+
89
+ return label
90
+
91
+ }()
92
+
93
+
94
+
95
+
96
+
97
+
98
+
21
- let editProfileFollowButton: UIButton = {
99
+ let editProfileFollowButton: UIButton = {
22
100
 
23
101
  let button = UIButton(type: .system)
24
102
 
@@ -26,7 +104,7 @@
26
104
 
27
105
  button.layer.borderColor = UIColor.lightGray.cgColor
28
106
 
29
- button.addTarget(self, action: #selector(handleEditProfileFollow), for: .editingChanged)
107
+ button.addTarget(self, action: #selector(handleEditProfileFollow), for: .touchUpInside)
30
108
 
31
109
  button.layer.borderWidth = 0.5
32
110
 
@@ -38,19 +116,109 @@
38
116
 
39
117
  }()
40
118
 
119
+
120
+
41
-
121
+ // MARK: - Handlers
42
-
122
+
123
+
124
+
43
- @objc func handleEditProfileFollow() {
125
+ @objc func handleEditProfileFollow() {
44
126
 
45
127
  delegate?.handleEditFollowTapped(for: self)
46
128
 
47
- }
129
+ }
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
48
-
143
+ func setUserStats(for user: User?) {
144
+
145
+
146
+
49
-
147
+ guard let uid = user?.uid else { return }
148
+
149
+
150
+
50
-
151
+ var numberOfFollowers: Int!
152
+
51
-
153
+ var numberOfFollowing: Int!
154
+
155
+
156
+
52
-
157
+ // get number of followers
158
+
159
+ USER_FOLLOWER_REF.child(uid).observeSingleEvent(of: .value) { (snapshot) in
160
+
161
+
162
+
163
+ if let snapshot = snapshot.value as? Dictionary<String, AnyObject> {
164
+
165
+ numberOfFollowers = snapshot.count
166
+
167
+ } else {
168
+
169
+ numberOfFollowers = 0
170
+
171
+ }
172
+
173
+
174
+
175
+ let attributedText = NSMutableAttributedString(string: "(numberOfFollowers!) \n", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)])
176
+
177
+ attributedText.append(NSAttributedString(string: "followers", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),
178
+
179
+ NSAttributedString.Key.foregroundColor: UIColor.lightGray]))
180
+
181
+ self.followersLabel.attributedText = attributedText
182
+
183
+ }
184
+
185
+
186
+
187
+ // get number of following
188
+
189
+
190
+
191
+ USER_FOLLOWING_REF.child(uid).observeSingleEvent(of: .value) { (snapshot) in
192
+
193
+
194
+
195
+ if let snapshot = snapshot.value as? Dictionary<String, AnyObject> {
196
+
197
+ numberOfFollowing = snapshot.count
198
+
199
+ } else {
200
+
201
+ numberOfFollowing = 0
202
+
203
+ }
204
+
205
+
206
+
207
+ let attributedText = NSMutableAttributedString(string: "(numberOfFollowing!) \n", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)])
208
+
209
+ attributedText.append(NSAttributedString(string: "following", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),
210
+
211
+ NSAttributedString.Key.foregroundColor: UIColor.lightGray]))
212
+
213
+ self.followingLabel.attributedText = attributedText
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
53
- func configureEditProfileFollowButton(){
221
+ func configureEditProfileFollowButton(){
54
222
 
55
223
 
56
224
 
@@ -60,13 +228,11 @@
60
228
 
61
229
 
62
230
 
63
- // ログインしてるユーザーIDと今、UserProfileHeaderにいるユーザーのIDが同じか 同じだったら、edit profileボタン
64
-
65
231
  if currentUid == user.uid {
66
232
 
67
233
  // configure button as edit profile
68
234
 
69
- editProfileFollowButton.setTitle("プロフィールを編集", for: .normal)
235
+ editProfileFollowButton.setTitle("テストプロフィールを編集", for: .normal)
70
236
 
71
237
  } else {
72
238
 
@@ -98,6 +264,58 @@
98
264
 
99
265
 
100
266
 
267
+ // MARK: - Init
268
+
269
+
270
+
271
+ override init(frame: CGRect) {
272
+
273
+ super.init(frame: frame)
274
+
275
+
276
+
277
+ addSubview(profileImageView)
278
+
279
+ profileImageView.anchor(top: self.topAnchor, left: self.leftAnchor, bottom: nil, right: nil, paddingTop: 16, paddingLeft: 12, paddingBottom: 0, paddingRight: 0, width: 80, height: 80)
280
+
281
+ profileImageView.layer.cornerRadius = 80 / 2
282
+
283
+
284
+
285
+ addSubview(nameLabel)
286
+
287
+ nameLabel.anchor(top: profileImageView.bottomAnchor, left: self.leftAnchor, bottom: nil, right: nil, paddingTop: 12, paddingLeft: 12, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
288
+
289
+
290
+
291
+ configureUserStats()
292
+
293
+
294
+
295
+ addSubview(editProfileFollowButton)
296
+
297
+
298
+
299
+ editProfileFollowButton.anchor(top: postsLabel.bottomAnchor, left: postsLabel.leftAnchor, bottom: nil, right: self.rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 0, paddingRight: 12, width: 0, height: 30)
300
+
301
+
302
+
303
+ configureBottomToolBar()
304
+
305
+ }
306
+
307
+
308
+
309
+ required init?(coder aDecoder: NSCoder) {
310
+
311
+ fatalError("init(coder:) has not been implemented")
312
+
313
+ }
314
+
315
+ }
316
+
317
+
318
+
101
319
  ```
102
320
 
103
321
  ###### 後にUserProfile.swiftでフォロー・アンフォローを実装したいので、Protocolsファイルに設定
@@ -140,7 +358,9 @@
140
358
 
141
359
 
142
360
 
361
+ import UIKit
362
+
143
- class UserProfileVC: UserProfileHeaderDelegate {
363
+ import Firebase
144
364
 
145
365
 
146
366
 
@@ -150,36 +370,208 @@
150
370
 
151
371
 
152
372
 
153
- // set delegate
154
-
155
- header.delegate = self
156
-
157
-
158
-
159
- self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
160
-
161
-
162
-
163
- // profileページのためにセルにしたいファイルを登録
164
-
165
- self.collectionView!.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier)
166
-
167
-
168
-
169
- // MARK: - UserProfileHeader Protocol
170
-
171
- func handleEditFollowTapped(for header: UserProfileHeader) {
373
+ class UserProfileVC: UICollectionViewController, UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate {
374
+
375
+
376
+
377
+
378
+
379
+ // MARK: - Properties
380
+
381
+
382
+
383
+
384
+
385
+ var currentUser: User?
386
+
387
+
388
+
389
+ var userToLoadFromSearchVC: User?
390
+
391
+
392
+
393
+ override func viewDidLoad() {
394
+
395
+ super.viewDidLoad()
396
+
397
+
398
+
399
+ // Uncomment the following line to preserve selection between presentations
400
+
401
+ // self.clearsSelectionOnViewWillAppear = false
402
+
403
+
404
+
405
+ // Register cell classes
406
+
407
+ self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
408
+
409
+
410
+
411
+ self.collectionView!.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier)
412
+
413
+
414
+
415
+ self.collectionView.backgroundColor = .white
416
+
417
+
418
+
419
+ if userToLoadFromSearchVC == nil {
420
+
421
+ fetchCurrentUserData()
422
+
423
+ }
424
+
425
+
426
+
427
+ }
428
+
429
+
430
+
431
+
432
+
433
+ // MARK: - UICollectionView
434
+
435
+
436
+
437
+ override func numberOfSections(in collectionView: UICollectionView) -> Int {
438
+
439
+ // #warning Incomplete implementation, return the number of sections
440
+
441
+ return 1
442
+
443
+ }
444
+
445
+
446
+
447
+
448
+
449
+ override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
450
+
451
+ // #warning Incomplete implementation, return the number of items
452
+
453
+ return 0
454
+
455
+ }
456
+
457
+
458
+
459
+ override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
460
+
461
+
462
+
463
+ // declare header
464
+
465
+ let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader
466
+
467
+
468
+
469
+ // set delegate
470
+
471
+ header.delegate = self
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+ if let user = self.currentUser {
480
+
481
+ // UserProfileHeader のuser
482
+
483
+ header.user = user
484
+
485
+ } else if let userToLoadFromSearchVC = self.userToLoadFromSearchVC {
486
+
487
+ header.user = userToLoadFromSearchVC
488
+
489
+ navigationItem.title = userToLoadFromSearchVC.username
490
+
491
+ }
492
+
493
+
494
+
495
+ // return header
496
+
497
+ return header
498
+
499
+ }
500
+
501
+ func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
502
+
503
+ return CGSize(width: view.frame.width, height: 200)
504
+
505
+ }
506
+
507
+
508
+
509
+ override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
510
+
511
+ let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
512
+
513
+
514
+
515
+ // Configure the cell
516
+
517
+
518
+
519
+ return cell
520
+
521
+ }
522
+
523
+
524
+
525
+ // MARK: - UserProfileHeader Protocol
526
+
527
+ func handleEditFollowTapped(for header: UserProfileHeader) {
172
528
 
173
529
  print("タップ")
174
530
 
175
531
  }
176
532
 
533
+
534
+
535
+ // MARK: - API
536
+
537
+ func fetchCurrentUserData() {
538
+
539
+ // set the user in header
540
+
541
+ guard let currentUid = Auth.auth().currentUser?.uid else { return }
542
+
543
+
544
+
545
+ Database.database().reference().child("users").child(currentUid).observeSingleEvent(of: .value) { (snapshot) in
546
+
547
+
548
+
549
+ guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
550
+
551
+ let uid = snapshot.key
552
+
553
+
554
+
555
+ let user = User(uid: uid, dictionary: dictionary)
556
+
557
+ self.currentUser = user
558
+
559
+ self.navigationItem.title = user.username
560
+
561
+ self.collectionView.reloadData()
562
+
563
+     }
564
+
565
+   }
566
+
567
+ }
568
+
569
+
570
+
177
571
  ```
178
572
 
179
573
 
180
574
 
181
- 今回の実装の為のコードを抜き出しました。
182
-
183
575
  これでコンソールに「タップ」と出力されて欲しいのですが、出ません。
184
576
 
185
577
  原因に検討がつく方や似たような実装をした方、もしいらっしゃいましたら、お力添え頂きたいです。

2

ツイあk

2019/02/19 08:07

投稿

amazon_106
amazon_106

スコア50

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,5 @@
1
+ タイトルのことをしたかったのですが、その前のテストで詰まっています。
2
+
1
3
  ##現在詰まっているところ
2
4
 
3
5
  followボタンをクリックし```print("タップ")```を出力したい

1

追加

2019/02/19 07:25

投稿

amazon_106
amazon_106

スコア50

test CHANGED
File without changes
test CHANGED
@@ -173,3 +173,13 @@
173
173
  }
174
174
 
175
175
  ```
176
+
177
+
178
+
179
+ 今回の実装の為のコードを抜き出しました。
180
+
181
+ これでコンソールに「タップ」と出力されて欲しいのですが、出ません。
182
+
183
+ 原因に検討がつく方や似たような実装をした方、もしいらっしゃいましたら、お力添え頂きたいです。
184
+
185
+ よろしくお願いします。