CollectionViewCell не отображается в collectionView - PullRequest
0 голосов
/ 16 февраля 2020

У меня проблема с тем, что пользовательский 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")
    }
}

СПАСИБО :)

1 Ответ

0 голосов
/ 18 февраля 2020

Проблема заключалась в том, что я ранее создал экземпляр FeedViewController с

FeedViewController(collectionViewLayout: UICollectionViewLayout())

вместо `FeedViewController (collectionViewLayout:

UICollectionViewFlowLayout ()))`, поскольку FeedViewController имеет

UICollectionViewDelegateFlowLayout протокол.

...