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") } }
回答1件
あなたの回答
tips
プレビュー