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

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

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

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

Xcode

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

Swift

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

Q&A

解決済

1回答

1915閲覧

scrollDirectionを1部のcollectionViewCellを適応したい。

退会済みユーザー

退会済みユーザー

総合スコア0

Firebase

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

Xcode

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

Swift

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

0グッド

0クリップ

投稿2018/04/18 08:13

編集2018/04/20 03:53

collectionViewCellを3つ縦に配置しています。
(グリーン,パープル,グレー)
イメージ説明

グレーのみ、scrollDirection = .horizontalを適応したいのですが、
override viewDidLoad内で私が定義すると3つ全てのセルが.horizontalの動きになってしまいます。
numberOfItemsInSectionなどの関数ではセルをif文のsectionで切り分けているので、
新たに関数を使い、if文のsectionでグレーのみにscrollDirectionを適応する方法を調べているのですが、
分からずにいます。

教えて頂ければ助かります。
よろしくお願いします。

追記しました。

//コントローラーです 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(CCCCell.self, forCellWithReuseIdentifier: "cellId3") collectionView?.register(BBBCell.self, forCellWithReuseIdentifier: "cellId2") collectionView?.register(AAACell.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! CCCCell case 1: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath) as! BBBCell default: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! AAACell } } override func numberOfSections(in collectionView: UICollectionView) -> Int { return 3 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if section == 2 { return 10 } else { return 1 } } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch indexPath.section { case 2: return CGSize(width: 50, height: 50) case 1: return CGSize(width: view.frame.width, height: 200) default: return CGSize(width: view.frame.width, height: 200) } } }
//ホリゾンタルにしたいグレーのCCCCellです。 import UIKit class CCCCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setupMenuBar() backgroundColor = .gray } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupMenuBar() { } }

//

画像を追加しました。
イメージ説明

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

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

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

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

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

guest

回答1

0

ベストアンサー

グレーの部分のセルにUICollectionViewが設定されているように見えますので、そのUICollectionViewのUICollectionViewFlowLayoutで指定すればできると思います。

【追記】

例えばaCellというカスタムセルの中にUICollectionViewがあるようなイメージだといかがでしょうか?

class aCell: UICollectionViewCell, UICollectionViewDataSource { var collectionView: UICollectionView? required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! setup() } override init(frame: CGRect) { super.init(frame: frame) setup() } private func setup() { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal collectionView = UICollectionView(frame: self.contentView.frame, collectionViewLayout: layout) collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") collectionView?.dataSource = self self.addSubview(collectionView!) } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) cell.backgroundColor = .yellow return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(50, 50, 50, 50) } }

【再追記】

例えば下記の箇所を変更するといかがでしょうか?

numberOfItemsInSectionでは1を返します。

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 1 }

sizeForItemAtでセルが1個になるのでサイズを少し大きくする(これは任意です。)

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch indexPath.section { case 2: // セルのサイズを少し大きくしています。 return CGSize(width: 300, height: 100) case 1: return CGSize(width: view.frame.width, height: 200) default: return CGSize(width: view.frame.width, height: 200) } }

CCCCell内でcollectionViewを作成する。

import UIKit class CCCCell: UICollectionViewCell, UICollectionViewDataSource { var collectionView: UICollectionView? override init(frame: CGRect) { super.init(frame: frame) setupMenuBar() backgroundColor = .gray } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setupMenuBar() { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .horizontal collectionView = UICollectionView(frame: self.contentView.frame, collectionViewLayout: layout) collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") collectionView?.dataSource = self self.addSubview(collectionView!) } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) cell.backgroundColor = .yellow return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(50, 50, 50, 50) } }

投稿2018/04/18 22:23

編集2018/04/19 21:25
newmt

総合スコア1277

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

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

退会済みユーザー

退会済みユーザー

2018/04/19 04:24 編集

ありがとうございます。 グレーのUICollectionViewCell内にcollectionViewは作成していませんので、 return10しているのはUICollectionViewCellだけになるのですが、 newmt様の仰る >>UICollectionViewが設定されている というのは、 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection ... の、collectionViewの部分を指しているのでしょうか?
newmt

2018/04/19 11:07

私のイメージではグレーの部分にセルを一つ用意してその中でUICollectionViewがあるような設定なのかなと思いました。雑で恐縮ですが、回答にイメージを追記しました。
退会済みユーザー

退会済みユーザー

2018/04/19 12:27

とんでもないです。こちらが教えて頂いている立場ですので、そう仰らないでください。 教えて頂いたイメージを参考に、セルにコードを書いて見ましたが、ホリゾンタルにできていません。 本文にコントローラーとグレーのセルのコードを追記いたしましたので、よかったら見ていただけないでしょうか?
newmt

2018/04/19 21:26

回答を追記しました。
退会済みユーザー

退会済みユーザー

2018/04/20 03:53 編集

ありがとうございます。 1つのセクションにreturn 20のセルを乗せて、横長になったセクションをホリゾンタルにスクロールする事ができました。とても勉強になりました。 私の説明不足などで恐縮なのですが、 現在グレーのセルを本文の様に return 10していまして、 return CGSizeをr(width: 50, height: 50) から➡️ (width: view.frame.width, height: view.frame.height)に変更しました。 グレーのセルを10個分バーティカルにスクロールできる状態にしています。 このグレーのセルのみ ページをフリックでめくる様に、10個分ホリゾンタルにスクロールしたいと思っていまして、 他のセクションとの切り分けをif文で行いたいです。 newmt様の仰る方法は、この切り分けの後にセクションにさらにセルを追加していくのに大変役立つ知識かと思うのですが、 まずはセクションを切り分けるための方法を探していました。 私の本文のHogeControllerのviewDidLoad内で切り分けることは可能でしょうか? 本文に画像を追加しました。 何度もすみません。
newmt

2018/04/20 11:38

「他のセクションとの切り分けをif文で行いたい」というのがよくわかっていないのですが、 セクションで読み込むセルが異なっている時点で、切り分けができているということではないということでしょうか? 仮に全てをHogeViewControllerのcollectionViewで処理しようとされているのでしたら、私は実装のイメージがつきません。すいません。
退会済みユーザー

退会済みユーザー

2018/04/22 15:39 編集

こちらこそ、本当にわかりづらい本文で申し訳ありません 何度もアドバイスを頂き、私の質問の実行したいことを今後発展させるための知識となりましたので、ベストアンサーとさせて頂きます。 改めて整理して新たな質問をしました。 見かけましたら、見ていただけると嬉しいです。 いつもありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問