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

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

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

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

Q&A

解決済

1回答

487閲覧

CollectionViewでCustomCellを使った際のLayout

atk_721

総合スコア62

Swift

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

0グッド

0クリップ

投稿2020/04/12 23:43

前提・実現したいこと

CustomCellを使用したCollectionViewでcellを横に3個並べて表示したいのですが、思った様にLayoutできません。
画像,2の様に等間隔で表示したいです。

画像、1
イメージ説明
画像、2
イメージ説明

CollectionViewCell

import UIKit import SDWebImage class OtherCollectionViewCell: UICollectionViewCell { @IBOutlet weak var otherView: UIImageView! @IBOutlet weak var otherNameLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } func toFields(user: UserModel){ self.otherView.sd_setImage(with: URL(string: user.userIcon), completed: nil) self.otherNameLabel.text = user.userName } }

CollectionView

import UIKit import Firebase class OtherCollectionViewController: UICollectionViewController { var userDatas = [UserModel]() var userRef: DatabaseReference = Database.database().reference().child("UsersProfile") override func viewDidLoad() { super.viewDidLoad() collectionView.register(UINib(nibName: "OtherCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "otherCell") collectionView.delegate = self collectionView.dataSource = self getUserData() } func getUserData(){ if UserDefaults.standard.object(forKey: "userID") != nil { let userID = UserDefaults.standard.object(forKey: "userID") as! String userDatas.removeAll() userRef.observe(.value) { (snapShot) in for snap in snapShot.children { let userData = UserModel(snapShot: snap as! DataSnapshot) self.userDatas.insert(userData, at: 0) self.collectionView.reloadData() } } }else{ print("userdefaults Error") } } // MARK: UICollectionViewDataSource override func numberOfSections(in collectionView: UICollectionView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of items return userDatas.count } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let otherCell = collectionView.dequeueReusableCell(withReuseIdentifier: "otherCell", for: indexPath) as! OtherCollectionViewCell let user = userDatas[indexPath.row] otherCell.toFields(user: user) return otherCell } }

Xcode上での設定

xibFileのcellはOtherCollectionViewCellを継承しています。OtherCollectionView内のCellもXcode上でつないでいます。
イメージ説明
xibFileからOtherCollectionViewCellの高さをMainStoryboard上のCellと高さを揃えています。(揃える必要があるのかわからないが揃えていますが、意味はあるのでしょうか?)
![イメイメージ説明イメージ説明

試したこと

https://qiita.com/tamagalago/items/a6bc26ab8574138280e9
上記のサイトを見ながら、実装してみたのですがLayoutが変わりませんでした。
こちらはCustomCellではないので、Layoutが変わらなかったのでしょうか?

補足情報(FW/ツールのバージョンなど)

swift5
xcode Version 11.4 (11E146)

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

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

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

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

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

guest

回答1

0

ベストアンサー

自動調整使わないのでしたら下記のような書き方でセルのサイズを設定できます。

extension OtherCollectionViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { // 40.0 はcollectionViewの左右のスペース(Selection Insets の値)とセル同士のスペース(Min SpaceingのFor cellsの値) // ↑何列にするかで調整する let width: CGFloat = (collectionView.frame.width - 40.0) / 3 let height: CGFloat = width // 正方形なら return CGSize(width: width, height: height) } }

投稿2020/04/13 05:50

TakuyaAso

総合スコア1361

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

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

atk_721

2020/04/13 07:30

回答ありがとうございます。Layoutを変更することができました。コードを動かして思い通りのLayoutに変更したいと思います!
TakuyaAso

2020/04/13 11:00

よかったです! CollectionViewはちょっとクセがありますよね。
atk_721

2020/04/13 15:17

tablviewと同じ様には行かず悩んでいたので、助かりました!UICollectionViewFlowLayoutを知る事ができ、理解が深まりました!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問