CollectionViewCell未出现在collectionView中

问题描述 投票:0回答:1

我有一个自定义UICollectionViewCell不会出现在UICollectionView中的问题。有人知道我在想什么吗?我预先感谢。

下面是我的UICollectionViewController:

private let reuseIdentifier = "FeedCell"

class FeedViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    // MARK: Properties
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.register(FeedCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    //MARK: - UICollectionViewFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = view.frame.width

        return CGSize(width: width, height: width)
    }


    //MARK: - UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    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! FeedCell
        print("CELL")
        cell.backgroundColor = .black

        return cell
    }


//FEED CELL 

class FeedCell: UICollectionViewCell {

    //MARK: - Properties

    let profileImageView: CustomeImageView = {
        let imageView = CustomeImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.backgroundColor = .lightGray

        return imageView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = .red
        print("feedcell1")
        addSubview(profileImageView)
        profileImageView.anchor(top: topAnchor, bottom: nil, left: leftAnchor, right: nil, paddingTop: 8, paddingBottom: 0, paddingLeft: 8, paddingRight: 0, width: 40, height: 40)
        profileImageView.layer.cornerRadius = 40/2
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

谢谢:)

uicollectionview uicollectionviewcell
1个回答
0
投票

问题是我之前使用]实例化了FeedViewController

FeedViewController(collectionViewLayout: UICollectionViewLayout())

而不是`FeedViewController(collectionViewLayout:

UICollectionViewFlowLayout()))`,因为FeedViewController具有

UICollectionViewDelegateFlowLayout协议。

© www.soinside.com 2019 - 2024. All rights reserved.