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

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

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

Delegatesとは、オブジェクト指向型プログラミングにおいて、あるオブジェクトの操作を一部の他のオブジェクトに代替させる手法のこと。オブジェクトは他のデリゲートに頼って関数を実行することができます。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

解決済

1回答

1284閲覧

UICollectionViewDelegateFlowLayoutの関数が呼び出されない

yasumaro

総合スコア67

Delegates

Delegatesとは、オブジェクト指向型プログラミングにおいて、あるオブジェクトの操作を一部の他のオブジェクトに代替させる手法のこと。オブジェクトは他のデリゲートに頼って関数を実行することができます。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2019/09/06 12:58

UICollectionViewDelegateFlowLayoutの下記の関数が呼び出されず困っています。
delegate,dataSouce をselfにしregisterも入れているのになぜ呼ばれないのでしょうか?
3日以上悩んでいます。。。お力を貸してください。よろしくお願いします。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { //呼び出されない return CGSize(width: collectionView.frame.width, height: 60) }
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CommentCell //呼び出されない print(cell) return cell }

上記二つの関数のファイル全体です。
CommentVC.swift

import UIKit import Firebase private let reuseIdentifier = "CommentCell" class CommentVC:UICollectionViewController,UICollectionViewDelegateFlowLayout{ //MARK: - properties override func viewDidLoad() { super.viewDidLoad() // self.collectionView.delegate = self self.collectionView.dataSource = self //background color collectionView.backgroundColor = .white //navigation title navigationItem.title = "Comment" //register cell class collectionView?.register(CommentCell.self, forCellWithReuseIdentifier: reuseIdentifier) print("view did load") } // MARK: - UICollectionView func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { //呼ばれない return CGSize(width: collectionView.frame.width, height: 60) } override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //呼び出される return 5 } override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CommentCell //呼ばれない print(cell) return cell } }

CommentCell.swift

import UIKit class CommentCell: UICollectionViewCell { let profileImageView:CustomImageView = { let iv = CustomImageView() iv.contentMode = .scaleAspectFit iv.clipsToBounds = true iv.backgroundColor = .lightGray return iv }() let commentLabel:UILabel = { let label = UILabel() //NSAttributedString とは、 UILabel や UITextField のテキスト(文字列)を装飾す let attributedText = NSMutableAttributedString(string: "name", attributes: [NSAttributedString.Key.font :UIFont.boldSystemFont(ofSize: 12)]) attributedText.append(NSAttributedString(string: "some test comment", attributes: [ NSAttributedString.Key.font :UIFont.systemFont(ofSize: 12)])) label.attributedText = attributedText return label }() // MARK: - Init override init(frame: CGRect) { super.init(frame: frame) print("conmment cell") addSubview(profileImageView) profileImageView.anchor(top: nil, left: leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 48, height: 48) profileImageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true profileImageView.layer.cornerRadius = 48 / 2 addSubview(commentLabel) commentLabel.anchor(top: nil, left: profileImageView.rightAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: 0, height: 0) commentLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2019/09/06 15:09 編集

UICollectionViewDelegateFlowLayoutのデリゲート先は設定してますか? と思ったら、collectionViewControllerですねこれ、
yasumaro

2019/09/06 15:11

はい。そうです。
guest

回答1

0

自己解決

collectionViewをUICollectionViewFlowLayout()で初期化していないのが原因でした。
他のviewControllerでタップ時にinstanceを生成しnavigationコントローラーで遷移する仕様でした。

func handleCommentTapped(for cell: FeedCell) { //ここがUICollectionViewFlowLayout()ではなくUICollectionViewLayout()になっていましたorz let commentVC = CommentVC(collectionViewLayout:UICollectionViewFlowLayout()) navigationController?.pushViewController(commentVC, animated: true) }

投稿2019/09/06 15:15

yasumaro

総合スコア67

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問