##現在詰まっているところ
followボタンをクリックしprint("タップ")
を出力したい
######ボタンを設置しているファイル
UserProfileHeader.swift import UIKit import Firebase class UserProfileHeader: UICollectionViewCell { // MARK: - Properties var delegate: UserProfileHeaderDelegate? var user: User? { didSet { // configure edit profile button configureEditProfileFollowButton() // set user stats setUserStats(for: user) let fullName = user?.name nameLabel.text = fullName profileImageView.loadImage(with: (user?.profileImageUrl)!) } } let profileImageView: UIImageView = { let iv = UIImageView() iv.contentMode = .scaleAspectFill iv.clipsToBounds = true iv.backgroundColor = .red return iv }() let nameLabel: UILabel = { let label = UILabel() label.font = UIFont.boldSystemFont(ofSize: 12) return label }() let editProfileFollowButton: UIButton = { let button = UIButton(type: .system) button.layer.cornerRadius = 3 button.layer.borderColor = UIColor.lightGray.cgColor button.addTarget(self, action: #selector(handleEditProfileFollow), for: .touchUpInside) button.layer.borderWidth = 0.5 button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14) button.setTitleColor(.black, for: .normal) return button }() // MARK: - Handlers @objc func handleEditProfileFollow() { delegate?.handleEditFollowTapped(for: self) } func setUserStats(for user: User?) { guard let uid = user?.uid else { return } var numberOfFollowers: Int! var numberOfFollowing: Int! // get number of followers USER_FOLLOWER_REF.child(uid).observeSingleEvent(of: .value) { (snapshot) in if let snapshot = snapshot.value as? Dictionary<String, AnyObject> { numberOfFollowers = snapshot.count } else { numberOfFollowers = 0 } let attributedText = NSMutableAttributedString(string: "(numberOfFollowers!) \n", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)]) attributedText.append(NSAttributedString(string: "followers", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray])) self.followersLabel.attributedText = attributedText } // get number of following USER_FOLLOWING_REF.child(uid).observeSingleEvent(of: .value) { (snapshot) in if let snapshot = snapshot.value as? Dictionary<String, AnyObject> { numberOfFollowing = snapshot.count } else { numberOfFollowing = 0 } let attributedText = NSMutableAttributedString(string: "(numberOfFollowing!) \n", attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14)]) attributedText.append(NSAttributedString(string: "following", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor.lightGray])) self.followingLabel.attributedText = attributedText } } func configureEditProfileFollowButton(){ guard let currentUid = Auth.auth().currentUser?.uid else { return } guard let user = self.user else { return } if currentUid == user.uid { // configure button as edit profile editProfileFollowButton.setTitle("テストプロフィールを編集", for: .normal) } else { // configure button as follow button editProfileFollowButton.setTitleColor(.white, for: .normal) editProfileFollowButton.backgroundColor = UIColor(red: 17/255, green: 154/255, blue: 237/255, alpha: 1) user.checkIfUserIsFollowed (completion: { (followed) in if followed { self.editProfileFollowButton.setTitle("following", for: .normal) } else { self.editProfileFollowButton.setTitle("follow", for: .normal) } }) } } // MARK: - Init override init(frame: CGRect) { super.init(frame: frame) addSubview(profileImageView) profileImageView.anchor(top: self.topAnchor, left: self.leftAnchor, bottom: nil, right: nil, paddingTop: 16, paddingLeft: 12, paddingBottom: 0, paddingRight: 0, width: 80, height: 80) profileImageView.layer.cornerRadius = 80 / 2 addSubview(nameLabel) nameLabel.anchor(top: profileImageView.bottomAnchor, left: self.leftAnchor, bottom: nil, right: nil, paddingTop: 12, paddingLeft: 12, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) configureUserStats() addSubview(editProfileFollowButton) 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) configureBottomToolBar() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
後にUserProfile.swiftでフォロー・アンフォローを実装したいので、Protocolsファイルに設定
Protocols.swift protocol UserProfileHeaderDelegate { func handleEditFollowTapped(for header: UserProfileHeader) }
print("タップ")を実装したファイル
UserProfile.swift import UIKit import Firebase private let reuseIdentifier = "Cell" private let headerIdentifier = "UserProfileHeader" class UserProfileVC: UICollectionViewController, UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate { // MARK: - Properties var currentUser: User? var userToLoadFromSearchVC: User? override func viewDidLoad() { super.viewDidLoad() // Uncomment the following line to preserve selection between presentations // self.clearsSelectionOnViewWillAppear = false // Register cell classes self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) self.collectionView!.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier) self.collectionView.backgroundColor = .white if userToLoadFromSearchVC == nil { fetchCurrentUserData() } } // MARK: - UICollectionView override func numberOfSections(in collectionView: UICollectionView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return 0 } override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { // declare header let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as! UserProfileHeader // set delegate header.delegate = self if let user = self.currentUser { // UserProfileHeader のuser header.user = user } else if let userToLoadFromSearchVC = self.userToLoadFromSearchVC { header.user = userToLoadFromSearchVC navigationItem.title = userToLoadFromSearchVC.username } // return header return header } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 200) } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) // Configure the cell return cell } // MARK: - UserProfileHeader Protocol func handleEditFollowTapped(for header: UserProfileHeader) { print("タップ") } // MARK: - API func fetchCurrentUserData() { // set the user in header guard let currentUid = Auth.auth().currentUser?.uid else { return } Database.database().reference().child("users").child(currentUid).observeSingleEvent(of: .value) { (snapshot) in guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return } let uid = snapshot.key let user = User(uid: uid, dictionary: dictionary) self.currentUser = user self.navigationItem.title = user.username self.collectionView.reloadData() } } }
この状態でfollowボタンを押しても、Thread 1: breakpoint 2.1もコンソール上にも反応がありませんでした。
回答2件
あなたの回答
tips
プレビュー