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

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

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

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

Xcode

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

Swift

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

Q&A

解決済

1回答

1403閲覧

sizeForItemAt indexPath内を、if文で分けたいです。

退会済みユーザー

退会済みユーザー

総合スコア0

Firebase

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

Xcode

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

Swift

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

0グッド

0クリップ

投稿2018/04/13 14:31

編集2018/04/14 05:29

下記コードにあるUICollectionViewControllerに、3つのUICollectionViewCellをレジスターしています。
これらをsizeForItemAt indexPath内で異なるCGSizeに設定したいのですが、
上手く設定するための方法が見つかりません。

(width: view.frame.width, height: 400)だったり、
(width: 200, height: 300)だったりと、
異なるCGSizeで設定したいです。

if文やindexPath.itemのcaseでの方法を探してはいるのですが、
なかなか設定できないため質問します。
教えて頂きたいです。

よろしくお願いします。

import UIKit class View1Controller: UICollectionViewController, UICollectionViewDelegateFlowLayout { private let cellId = "cellId" private let cellId2 = "cellId" private let cellId3 = "cellId" override func viewDidLoad() { super.viewDidLoad() let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.minimumInteritemSpacing = 0 layout.minimumLineSpacing = 0 collectionView?.collectionViewLayout = layout collectionView?.register(HogeCell.self, forCellWithReuseIdentifier: "cellId") collectionView?.register(HugaCell.self, forCellWithReuseIdentifier: "cellId2") collectionView?.register(aCell.self, forCellWithReuseIdentifier: "cellId3") } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { switch indexPath.item { case 2: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId3", for: indexPath) as! aCell case 1: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! HogeCell default: return collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath) as! HugaCell } } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.width, height: 300) } }

//追記しました。 こちらになります。

//AppDelegateです import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let layout = UICollectionViewFlowLayout() let view1Controller = View1Controller(collectionViewLayout: layout) window?.rootViewController = UINavigationController(rootViewController: view1Controller) return true } func applicationWillResignActive(_ application: UIApplication) { } func applicationDidEnterBackground(_ application: UIApplication) { } func applicationWillEnterForeground(_ application: UIApplication) { } func applicationDidBecomeActive(_ application: UIApplication) { } func applicationWillTerminate(_ application: UIApplication) { } }

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

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

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

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

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

guest

回答1

0

ベストアンサー

下記のような形でできませんでしょうか?

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { switch indexPath.item { case 2: return CGSize(width: 200, height: 300) case 1: return CGSize(width: view.frame.width, height: 400) default: return CGSize(width: 100, height: 100) } }

イメージ説明

投稿2018/04/13 21:59

編集2018/04/14 03:35
newmt

総合スコア1277

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

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

退会済みユーザー

退会済みユーザー

2018/04/14 03:23 編集

newmt様 ありがとうございます。 教えていただいたswitch文でRUNするとエラーは無いですが、画面がブラックアウト状態です。 cellForItemAt indexPathの様にcaseにUICollectionViewCellがas!されていないと判別できないのではと思い、色々と探していたのですが、見つからないです。 UICollectionViewCellのインスタンスをsizeforItemAt内に作成したりするのでしょうか?
newmt

2018/04/14 03:37

私の方ですと追加した画像のように表示されますが、ご質問に記載のコード以外に他に何か設定しているものはありますでしょうか? 「cellForItemAt indexPathの様にcaseにUICollectionViewCellがas!されていないと判別できないのではと思い、色々と探していたのですが、見つからないです。」 これは必要ないと思います。
退会済みユーザー

退会済みユーザー

2018/04/14 04:03 編集

UICollectionViewControllerのプロトコルに UICollectionViewDelegateFlowLayout を追加しています。 また、 cellのプロトコルに UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout を追加しています。 あとは、特に原因になっている箇所はないかと思います...
newmt

2018/04/14 04:11

cellのプロトコルに UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout を追加しています。 まず、これを外してシンプルにセルを表示するだけにした場合どうなりますでしょうか?
退会済みユーザー

退会済みユーザー

2018/04/14 04:28 編集

外しても変わらずブラックアウトしています。 各3つのセルは下記のコードで、カラーとファイル名違いです。 import UIKit class HugaCell: UICollectionViewCell { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } override init(frame: CGRect) { super.init(frame: frame) self.contentView.backgroundColor = .orange } }
newmt

2018/04/14 05:12 編集

View1Controller この名前はViewControllerではなく意図的にView1Controllerですか? ここの整合性が取れていないのではないかと思いました。
退会済みユーザー

退会済みユーザー

2018/04/14 05:16

はい 意図的です。 AppDelegateでは、 let layout = UICollectionViewFlowLayout() let view1Controller = View1Controller(collectionViewLayout: layout) window?.rootViewController = UINavigationController(rootViewController: view1Controller) でイニシャルに設定しています。
newmt

2018/04/14 05:23 編集

ちょっと気になったのですが、AppDelegateを見せていただくことはできませんでしょうか? また、storyboardは使っていないということでよろしいでしょうか?(もしかしてそこに指定されているクラス名がViewControllerのままだったりしていることが原因なのかと思いまして)
退会済みユーザー

退会済みユーザー

2018/04/14 05:28 編集

ストーリーボード未使用です 分かりづらく申し訳ないです。 AppDelegate追記しました。見て頂きたいです お願いします。
newmt

2018/04/14 05:50 編集

AppDelegateの記載がいくつか足りていません。 ひとまずdidFinishLaunchingWithOptionsを下記に変更するといかがでしょうか? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) let layout = UICollectionViewFlowLayout() let view1Controller = View1Controller(collectionViewLayout: layout) window?.rootViewController = UINavigationController(rootViewController: view1Controller) window?.makeKeyAndVisible() return true } 下記を参考にしてみてください。 https://qiita.com/lovee/items/5e8c7752d7e383660543
退会済みユーザー

退会済みユーザー

2018/04/14 06:27

AppDelegateの不足していた部分を加えたところ、if文でCellを分けることができました。 お手数おかけしてすみません!確認不足でした。 このnewmt様のコメントで解決できましたので、こちらをベストアンサーとさせて頂きます。 大変お世話になりました。ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問