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

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

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

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

Q&A

解決済

1回答

918閲覧

CollectionViewCell導入でThread1 エラーが出る

tyaritta

総合スコア12

Swift

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

0グッド

0クリップ

投稿2019/01/23 15:42

編集2019/01/24 09:00

#実現したいこと
CollectionViewを導入したい。

Swiftで、CollectionViewを導入したいと考えております。
https://hayashi-rin.net/post-1157/amp
のサイト様の記述を参考にしながら作成したのですが、
プレビューするとThread1エラーが出てしまいます。

今回のアプリの補足としましては、
・NavigationControllerを導入している。
・アプリを開いたページから遷移して、CollectionViewのページへ移動
→この際にエラーが出てしまう。

発生している問題・エラーメッセージ

Thread 1: signal SIGABRT アプリ立ち上げ時からのconsoleは下記です。 2019-01-24 17:53:24.410338+0900 test3[62641:16194225] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2019-01-24 17:53:24.412006+0900 test3[62641:16194225] [MC] Reading from public effective user settings. 2019-01-24 17:53:33.247386+0900 test3[62641:16194225] -[test3.SecondViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x104d1b9a0 2019-01-24 17:53:33.248254+0900 test3[62641:16194225] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[test3.SecondViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x104d1b9a0' *** First throw call stack: (0x1cb5ebef8 0x1ca7b9a40 0x1cb503154 0x1f85665c0 0x1cb5f1810 0x1cb5f34bc 0x1f7e3dbb4 0x1f7e3fc18 0x1f7e498c4 0x1f7e4b82c 0x1f7e45680 0x1f7e3dea4 0x1f7e3e6e4 0x1f7e1256c 0x1f89dbf44 0x1cfbcda34 0x1cfbd29c4 0x1cfb319d4 0x1cfb602f4 0x1cfb6115c 0x1cb579b94 0x1cb574828 0x1cb574dc8 0x1cb5745b8 0x1cd7e8584 0x1f8538bc8 0x1025e5448 0x1cb034b94) libc++abi.dylib: terminating with uncaught exception of type NSException

該当のソースコード

Swift4

1import UIKit 2 3class CollectionViewCell: UICollectionViewCell { 4 5 @IBOutlet var title:UILabel! 6 @IBOutlet var contents:UILabel! 7 8 override init(frame: CGRect) { 9 super.init(frame:frame) 10 } 11 12 required init?(coder aDecoder: NSCoder) { 13 super.init(coder:aDecoder)! 14 } 15 16 func collectionView(collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 17 let cell:CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell 18 cell.title.text = "title(indexPath.row)" 19 cell.contents.text = "text(indexPath.row)" 20 return cell 21 } 22 23 func numberOfSectionsIncollectionView(collectionView: UICollectionView) -> Int { 24 return 1 25 } 26 27 func collectionView(collectionView:UICollectionView,numberOfItemsInSection section:Int) -> Int{ 28 return 20 29 } 30 31 32}

試したこと

・StoryBoardとプログラムの接続で注意マークなどは出ていない
・identifierも設定できている。(cell)
デリゲートの設定がうまくいっていないのか、はたまたプログラミングの記述がおかしいのか・・・
3時間ほどネットで調べて見たのですが、うまい解法が浮かびませんでした・・・
イメージ説明

アドバイスいただけますと幸いです。
何卒よろしくお願いいたします。

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

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

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

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

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

fuzzball

2019/01/24 00:51

Consoleに表示されているエラーメッセージを書いて下さい。
tyaritta

2019/01/24 09:01

迅速なご確認ありがとうございます。 コンソールを追記致しましたので、ご確認頂けますと幸いです。 よろしくお願い致します。
guest

回答1

0

ベストアンサー

以下のメソッドはSecondViewControllerに書いて下さい。

collectionView(:cellForItemAt:)
numberOfSections(in:)
collectionView(
:numberOfItemsInSection:)

投稿2019/01/24 09:28

fuzzball

総合スコア16731

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

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

tyaritta

2019/01/24 13:35

迅速な御確認、ありがとうございます! CollectionViewCellのメソッドを消し、 SecondViewControllerに下記のように記載しましたが、Thread1エラーが出てしまいました。 import UIKit class SecondViewController: UIViewController{ @IBOutlet var risou: UILabel! var box = String() override func viewDidLoad() { super.viewDidLoad() risou.text = box } func collectionView(collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell:CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell cell.title.text = "title(indexPath.row)" cell.contents.text = "text(indexPath.row)" return cell } func numberOfSectionsIncollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView:UICollectionView,numberOfItemsInSection section:Int) -> Int{ return 20 } } 下記のようにコンソールに出力されます。 2019-01-24 22:23:27.479371+0900 test3[62809:16236296] -[test3.SecondViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x10381bb00 2019-01-24 22:23:27.482608+0900 test3[62809:16236296] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[test3.SecondViewController collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x10381bb00' 原因わかればご教授いただけますと幸いです。 お手数をおかけしますが、どうぞ宜しくお願い致します。
fuzzball

2019/01/25 00:28

参考にしている記事が古く、Swift2時代の書き方になっています。 Xcodeの補完機能を使うなどして修正して下さい。 例えば一番上のメソッドであれば、 cellforあたりまで入力すれば補完してくれます。 今後は一年以内くらいをメドに、なるべく新しい記事を参考にしましょう。(最低でもSwift3) また一つの記事だけを見るのではなく、複数の記事を見るようにするのがいいです。
fuzzball

2019/01/25 00:35

補足です。 プロトコルの指定をしておかないと補完されないかも知れません。(下記の「UICollectionViewDataSource」の部分) class SecondViewController: UIViewController, UICollectionViewDataSource {
tyaritta

2019/01/25 15:37

ありがとうございます! おかげさまで何とか実装できました。 ご回答いただきました通り、 ①補完機能を使って、collectionView関数を記載 ②UICollectionViewDataSourceを、プロトコルに指定 これらを実装することでエラーが解消されました…!ありがとうございます! 記事読む際は、バージョンに気を付けること、複数の記事を参考に記載することを心がけて今後も活動を続けてまいります。 ご丁寧に対応いただきましてありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問