CollectionViewCell 未出現在 collectionView 中 (CollectionViewCell not appearing in collectionView)


問題描述

CollectionViewCell 未出現在 collectionView 中 (CollectionViewCell not appearing in collectionView)

我有一個問題,自定義 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:

The issue was I had previously instantiated FeedViewController with

FeedViewController(collectionViewLayout: UICollectionViewLayout())

instead of `FeedViewController(collectionViewLayout:

UICollectionViewFlowLayout()))` since FeedViewController has

UICollectionViewDelegateFlowLayout protocol.

(by Nouru MunezaNouru Muneza)

參考文件

  1. CollectionViewCell not appearing in collectionView (CC BY‑SA 2.5/3.0/4.0)

#uicollectionviewcell #uicollectionview






相關問題

Sel UICollectionView tidak terlihat (UICollectionView cells are invisible)

UICollectionViewCell裡面的UIWebView不調用Delegate方法 (UIWebView inside UICollectionViewCell does not call Delegate Method)

Bộ sưu tập xem vấn đề thay đổi kích thước iPhone 4 và iPhone 5 (Collection view resizing issue iPhone 4 vs iPhone 5)

集合視圖單元格未出現 (Collection View Cells not appearing)

UICollectionView 單元格大小 ContentView 問題 (UICollectionView cell size ContentView Issue)

在 Swift 中使用 CollectionView 進行網格佈局 (Grid layout with CollectionView in Swift)

如何為collectionviewcell的刪除自定義動畫? (How to custom animation for collectionviewcell's deletion?)

檢測位於另一個 uicollectionview 內的 uicollectionView 的哪個 UICollectionViewCell 在中心 (Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center)

navigationController?.pushViewController 不工作 (navigationController?.pushViewController is not working)

XCode UICollectionViewCell 在模擬器上看起來比在故事板中小得多 (XCode UICollectionViewCell appearing much smaller on simulator than in storyboard)

CollectionViewCell 未出現在 collectionView 中 (CollectionViewCell not appearing in collectionView)

由 LPLinkView 和 UIImageView 組成的單元格的 CollectionView 速度很慢,並且在滾動時會重新加載數據 (CollectionView with Cells made up of LPLinkView's and UIImageView's is Slow and Reloads Data While Scrolling)







留言討論