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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

1553閲覧

collectionViewCell switch文でdequeueしたいです。

退会済みユーザー

退会済みユーザー

総合スコア0

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2018/04/10 14:43

編集2018/04/11 10:37

いつもお世話になっています。

現在のコードは簡単に表現すると、このような形になっています。


・クラスUICollectionViewCell "hoge" {

private let cellId = "cellId"

UICollectionView①作成

func {

UICollectionView①をaddsubView,constraintsなど

クラスUICollectionViewCell①をレジスター

}

//下記コード がここに入ります

}

・クラスUICollectionViewCell① {

func {
addsubView,constraintsなど

}

}

//下記コード //UICollectionViewCell "hoge"内 ... func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 30 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! Cell } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 200, height: frame.height - 10) } ...

上記のように、
UICollectionViewCell "hoge"内にUICollectionView①を作り、
クラスUICollectionViewCell①をregisterしています。

シミュレーターで見ると、UICollectionView①の上にUICollectionViewCell①が乗っているような形になっています。


今回行いたいのは、
UICollectionViewCell "hoge"内に
UICollectionView②を作り、
UICollectionViewCell②をregisterして
それをswitchで使い分けたいと考えています。


・クラスUICollectionViewCell "hoge" {

private let cellId = "cellId"
var cellBId = "cellId"

UICollectionView①作成
UICollectionView②作成

func {

UICollectionView①をaddsubView,constraintsなど
UICollectionView②をaddsubView,constraintsなど

クラスUICollectionViewCell①をレジスター
クラスUICollectionViewCell②をレジスター

}

//下記コード がここに入ります

}

・クラスUICollectionViewCell① {

func {
addsubView,constraintsなど

}

・クラスUICollectionViewCell② {

func {
addsubView,constraintsなど

}
}


```ここに言語を入力 //下記コード //UICollectionViewCell "hoge"内 ... func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 30 } //スイッチ func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch indexPath.item { //UICollectionViewCell②の場合 case 1: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellBId", for: indexPath) as! CellB default: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! Cell } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 200, height: frame.height - 10) } ...

UICollectionViewCell②はdequeueのみ行いたいので、その他(numberOfItemsInSectionやsizeForItemAt indexPath)は行いたくありません。

現在、エラー
'could not dequeue a view of kind: UICollectionElementKindCell with identifier cellBId - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' が発生します。

正しいswitch文で、それぞれdequeueすることが可能でしたらご指摘いただきたいです。

よろしくお願いします。

//追記
イメージ説明
CollectionView②(グリーン)の上にCollectionViewCell②(オレンジ)が乗っています。
numberOfItemsInSectionのreturnは1です。
CollectionViewCell②内にはUIImageView(目が光っているやつ)のみ作成。

CollectionView①(レッド)の上にCollectionViewCell①(イエロー)が乗っています。
numberOfItemsInSectionのreturnは3です。
CollectionViewCell①内にはUIImageView(ガンディー)のみ作成。

画像の通り(目が光っているやつ)が後ろに回り込んでしまったりと、混ざってしまいます。

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

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

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

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

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

guest

回答1

0

ベストアンサー

いまいち状況がつかめていないのですが、
「新たにUICollectionView②を作り、そこにまた新たなUICollectionViewCell②をregisterする」
ということはcellBIdとcellIdが登録されているUICollectionViewがそれぞれ違っているので、
cellForItemAtのcollectionViewはUICollectionView①またはUICollectionView②となり、同じcollectionViewの中ではswitchできないのではないでしょうか?

投稿2018/04/10 21:32

newmt

総合スコア1277

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

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

退会済みユーザー

退会済みユーザー

2018/04/11 02:58

newmt様 ありがとうございます。 私の本文の内容が大変おかしかったので大幅に修正しました。 お手数おかけして申し訳ありません。 もう1度本文を見ていただけないでしょうか?
退会済みユーザー

退会済みユーザー

2018/04/11 03:01

さらに本文修正しました。
newmt

2018/04/11 04:24

UICollectionView②は必要でしょうか? UICollectionView①上でUICollectionViewCell①とUICollectionViewCell②をswitchさせれば良いのように思えるのですが、やりたいことが違いますでしょうか?
退会済みユーザー

退会済みユーザー

2018/04/11 05:08 編集

はい。UICollectionViewCell①とUICollectionViewCell②の挙動と役割が全く違うので、コンテナで分けるように、UICollectionViewを分けたいと思っています。
newmt

2018/04/11 05:52

ちょっと実現されたいことを勘違いしていました。すいません。 別で一点確認なのですが、 could not dequeue a view of kind: UICollectionElementKindCell with identifier bcellIdに出てくる、 「bcellId」はどこで設定していますでしょうか? 「cellBId」や「cellId」と一致していないのでちょっと気になりました。
退会済みユーザー

退会済みユーザー

2018/04/11 05:56 編集

すみません、 bcellId = cellBIdです。 bcellIdだとわかりづらいと思い、本文ではcellBIdに変えていました、失礼しました。 これらは同一のものです。
newmt

2018/04/11 06:23

collectionView.dequeueReusableCellのcollectionViewを switchの分岐でUICollectionViewCell①とUICollectionViewCell②に変えた場合どうなりますでしょうか? //UICollectionViewCell①はcollectionView1 //UICollectionViewCell②はcollectionView2 //で宣言されているとします switch indexPath.item { //UICollectionViewCell②の場合 case 1: return collectionView2.dequeueReusableCell(withReuseIdentifier: "cellBId", for: indexPath) as! CellB default: return collectionView1.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! Cell }
退会済みユーザー

退会済みユーザー

2018/04/11 06:48 編集

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let BCell② = collectionView2 let ACell① = collectionView1 switch indexPath.item { case 1: return collectionView2.dequeueReusableCell(withReuseIdentifier: "bcellId", for: indexPath) as! BCell② default: return collectionView1.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ACell① } } このように定義するということでしょうか?
newmt

2018/04/11 06:53

いえ、UICollectionViewCell "hoge"の中のUICollectionView①作成 、UICollectionView②作成でそれぞれのcollectionViewを変数に保持していると思うのですが、その変数をcollectionViewの代わりに設定するということです。
退会済みユーザー

退会済みユーザー

2018/04/11 08:51

newmt様の仰る方法で設定しました。 クラスUICollectionViewCell①内にはUIImageView①を、 クラスUICollectionViewCell②内にはUIImageView②を作成しているのですが、 UICollectionViewを分けているのにシミュレーター上で そのUIImageView①と②が混ざってしまいます。 cellForItemAt indexPathでif文でreturn数を分けても、UIImageViewが混ざってしまいます。
退会済みユーザー

退会済みユーザー

2018/04/11 09:02

ただいま本文に画像を追加します。
退会済みユーザー

退会済みユーザー

2018/04/11 09:11

画像と本文を追記しました。 長くなって申し訳ないです。 見ていただけると幸いです。
newmt

2018/04/11 11:09

UICollectionView①を表示する場合はUICollectionView②を非表示、 UICollectionView②を表示する場合はUICollectionView①を非表示 とした場合はどうなりますでしょうか? また、実現されたいこと下記のようなものに近いでしょうか? https://github.com/irfanlone/Collection-View-in-a-collection-view-cell なんとなく、UICollectionViewCell "hoge"の他にUICollectionViewCell "huga"を作成し、 UICollectionViewCell "hoge"にUICollectionView① UICollectionViewCell "huga"にUICollectionView②と分けた方が良いような気がしています。
退会済みユーザー

退会済みユーザー

2018/04/11 16:20

newmt様 自分なりにずっと試行錯誤したのですが、未だ実現できません。 別の質問を作りましたので、もしお時間がありましたらそちらを是非見て頂きたいです。newmt様にアドバイスを頂きたいです。 ご提供いただいたgithubのドキュメント拝見しました ストーリボードを使用している様ですのであまり分かりませんでしたが、実現したいものに近いと思います。 >>他にUICollectionViewCell "hugaを作成し、 こちらの方法を参考に進めていこうと思います。 本当にありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問