コレクションビューでindexPathごとに使用するカスタムセルを切り分けたいと考えております。
現在は、全てのindexPathで同じカスタムセル名でnibを作成し、reusecellとして登録しています。
作ろうとしているものがセルごとに画像やボタンの配置が違ったりするので、画面ごとにxibを作成して、使い分けたいです。
nibを複数登録し、セルのデータを返すメソッドでindexPathが0のときは・・・と分岐してみましたが、上手くいきませんでした。
そもそも目的を達成するためには、この方法ではないほうが良いのでしょうか?
let screenNames = ["screenName01","screenName02","screenName03".....] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.automaticallyAdjustsScrollViewInsets = false // セルのNibを登録 self.collectionView!.register(UINib(nibName: "CollectionViewCell1", bundle: nil), forCellWithReuseIdentifier: "cell1") self.collectionView!.register(UINib(nibName: "CollectionViewCell2", bundle: nil), forCellWithReuseIdentifier: "cell2") } (中略) //データを返す override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell1: CollectionViewCell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath as IndexPath) as! CollectionViewCell1 let cell2: CollectionViewCell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath as IndexPath) as! CollectionViewCell2 if(indexPath.row == 0) cell1.mainImage.image = UIImage(named: screenNames[indexPath.row]) return cell1 } else { cell2.mainImage.image = UIImage(named: screenNames[indexPath.row]) return cell2 } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/02/08 12:50 編集
退会済みユーザー
2017/02/08 13:05
2017/02/08 22:11
退会済みユーザー
2017/02/08 23:52
2017/02/09 00:22 編集
退会済みユーザー
2017/02/09 00:43
2017/02/09 01:00
退会済みユーザー
2017/02/09 01:08
2017/02/09 01:30
退会済みユーザー
2017/02/09 01:37