いつもお世話になっています。
現在のコードは簡単に表現すると、このような形になっています。
・クラス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(ガンディー)のみ作成。
画像の通り(目が光っているやつ)が後ろに回り込んでしまったりと、混ざってしまいます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/11 02:58
退会済みユーザー
2018/04/11 03:01
2018/04/11 04:24
退会済みユーザー
2018/04/11 05:08 編集
2018/04/11 05:52
退会済みユーザー
2018/04/11 05:56 編集
2018/04/11 06:23
退会済みユーザー
2018/04/11 06:48 編集
2018/04/11 06:53
退会済みユーザー
2018/04/11 08:51
退会済みユーザー
2018/04/11 09:02
退会済みユーザー
2018/04/11 09:11
2018/04/11 11:09
退会済みユーザー
2018/04/11 16:20