Storyboard上で作業してないので、IBOutlet、IBActionの接続問題ではありません。
ただ、行った作業的にUI部品とコードの接続に問題があるんだと思います。
ユーザーページにheader sectionを作ろうと、デモのUI部品の設置を試みました。
元のユーザーページ
import UIKit import Firebase private let reuseIdentifier = "Cell" class UserProfileVC: UICollectionViewController { // MARK: - Properties override func viewDidLoad() { super.viewDidLoad() // Register cell classes self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) // fetch user data fetchCurrentUserData() } // MARK: - UICollectionView override func numberOfSections(in collectionView: UICollectionView) -> Int { // #warning Incomplete implementation, return the number of sections return 0 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return 0 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) return cell } // MARK: -API func fetchCurrentUserData(){ guard let currentUid = Auth.auth().currentUser?.uid else { return } Database.database().reference().child("users").child(currentUid).child("username").observeSingleEvent(of: .value) { (snapshot) in guard let username = snapshot.value as? String else { return } self.navigationItem.title = username } } }
UI部品の為のファイルをユーザーページの同じ階層に追加し以下のコードを書きました。
import UIKit class UserProfileHeader: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) self.backgroundColor = .red } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
編集したユーザーページのコード
import UIKit import Firebase private let reuseIdentifier = "Cell" #####追加 private let headerIdentifier = "UserProfileHeader" ######UICollectionViewDelegateFlowLayoutを追加 class UserProfileVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { // MARK: - Properties override func viewDidLoad() { super.viewDidLoad() // Register cell classes self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) ######追加 self.collectionView!.register(UserProfileHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerIdentifier) // background color self.collectionView?.backgroundColor = .white ######追加ここまで // fetch user data fetchCurrentUserData() } // MARK: - UICollectionView override func numberOfSections(in collectionView: UICollectionView) -> Int { #######追加 return 0から return 1に変更 return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return 0 } #######追加 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 200) } 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 // return header return header } ######追加ここまで override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) return cell } // MARK: -API func fetchCurrentUserData(){ guard let currentUid = Auth.auth().currentUser?.uid else { return } Database.database().reference().child("users").child(currentUid).child("username").observeSingleEvent(of: .value) { (snapshot) in guard let username = snapshot.value as? String else { return } self.navigationItem.title = username } } }
シミュレーターをビルドしたところユーザーページが開かず、
AppDelegate.swiftのclass AppDelegate周辺でThread 1: signal SIGABRTが表示されました。
xcodeのconsoleのエラーメッセージです。
reason: 'could not dequeue a view of kind: UICollectionElementKindSectionFooter with identifier UserProfileHeader - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
よろしくお願いします。
解決しました。
https://teratail.com/questions/164025
こちらでfuzzballさんが答えていることを参考にコードを見直してみたら、
HederとすべきところをFooterとしていました。
回答1件
あなたの回答
tips
プレビュー