ストーリーボード未使用で開発しています。
collectionViewControllerにcollectionViewCellを、
3つ縦に乗せて配置しています。
(グリーン,パープル,グレー)
numberOfItemsInSection内でグレーのみ return 3を返し、
グリーン,パープルはreturn 1を返しています。
ですので画像では見えないですが、下へスクロールすれば
グレーがverticalにreturn 3スクロールできます。
今回はこのverticalをhorizontalに変更して、
グレーのみページをめくる様に3回 横スクロールしたいです。
グリーン、パープルはそのまま固定で動かしません。
●やったこと
cellForItemAt indexPath内でif文を作り、
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal
を定義し,case 2:のGrayCellの部分に書きましたが、
RUNしたシミュレーター画面ではまずグリーンのみ現れ、
それを横スクロールすればパープル、
それを横スクロールすればグレー、次もグレー次もグレーと
sectionで分けているつもりが、
全て1つ繋がりでhorizontalになってしまいます。
グリーン、パープルはこの画像の様に固定して、
グレーだけを横スクロールで動かせる様にしたいです。
数日調べていますが、どうしても分からない為
どんなコーディングかをどうか教えて頂きたいです。
お願い申し上げます。
//コントローラーです import UIKit class HogeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { let cellId3 = "cellId" let cellId2 = "cellId" let cellId = "cellId" override func viewDidLoad() { super.viewDidLoad() collectionView?.backgroundColor = .white let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.minimumLineSpacing = 0 layout.minimumInteritemSpacing = 0 collectionView?.register(GlayCell.self, forCellWithReuseIdentifier: "cellId3") collectionView?.register(PurpleCell.self, forCellWithReuseIdentifier: "cellId2") collectionView?.register(GreenCell.self,forCellWithReuseIdentifier: "cellId") } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch indexPath.section { case 2: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath) as! GlayCell case 1: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath) as! PurpleCell default: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! GreenCell } } override func numberOfSections(in collectionView: UICollectionView) -> Int { return 3 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if section == 2 { return 3 } else { return 1 } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch indexPath.section { case 2: return CGSize(width: view.frame.width, height: view.frame.height) case 1: return CGSize(width: view.frame.width, height: 200) default: return CGSize(width: view.frame.width, height: 200) } } }
//ホリゾンタルにしたいグレーのGlayCellです。 //グリーン、パープルも同じコーディングです。 import UIKit class GlayCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setup() backgroundColor = .gray } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setup() { } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/04/23 14:48