質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1254閲覧

Thread 1: signal SIGABRT(IBOutlet、IBActionの接続以外の問題で)

amazon_106

総合スコア50

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/01/29 05:55

編集2019/01/29 06:25

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)

よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2019/01/29 06:02 編集

Consoleにエラーメッセージが表示されていませんか?
amazon_106

2019/01/29 06:03

忘れてました。追加しておきます。
amazon_106

2019/01/29 06:29 編集

解決しました。
guest

回答1

0

自己解決

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize { return CGSize(width: view.frame.width, height: 200) }

referenceと書いて、上記のコードを出しましたが、僕の場合、referenceSizeForHeaderInSectionを選択しないといけませんでした。

投稿2019/01/29 06:34

編集2019/01/29 06:58
amazon_106

総合スコア50

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzzball

2019/01/29 06:37

コードが Footer のままです。
amazon_106

2019/01/29 06:41

僕の場合はHeaderですが、あとでこの質問を見る方は Header=>Footerにする必要があるかもしれないので、 上記のコードは、「この部分が間違っていました。」にとどめる為、敢えてこの書き方にしました。
fuzzball

2019/01/29 06:48

「解決した方法」だから解決した方法書かないと意味がないのでは。
fuzzball

2019/01/29 06:49

「質問への追記・修正、ベストアンサー選択の依頼」にしか解決方法が書かれていないのでは、見た人は訳がわからないと思います。
amazon_106

2019/01/29 06:58

編集したので、後学の為になっているかどうかご意見下さい。
fuzzball

2019/01/29 07:05

問題ないと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問