2017-06-17 67 views
-1

我不明白爲什麼我的細胞沒有顯示:細胞沒有出現在UICollectionView

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

    let cellId = "pouet" 

    lazy var customCollectionView : UICollectionView = { 
     let layout = UICollectionViewLayout() 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.backgroundColor = UIColor.gray 
     cv.dataSource = self 
     cv.delegate = self 

     return cv 
    }() 

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

     customCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId) 
     addSubview(customCollectionView) 
     addConstraintsWithFormat(format: "H:|[v0]|", views: customCollectionView) 
     addConstraintsWithFormat(format: "V:|[v0]|", views: customCollectionView) 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 4 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = customCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 
     print("test") 

     return cell 
    } 

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

現在我print("test")沒有顯示任何,這意味着該方法實際上是不叫的吧?那麼,什麼是錯的?

+0

這裏是調試工具的屏幕截圖:https://tof.cx/image/FfbH1 – petaire

+0

把東西放在你的單元格中,否則沒有任何東西可以顯示 – Scriptable

+0

Stil,有一個默認的黑色背景,默認的寬度和高度,所以應該有東西來展示。而且,它會打印'測試'。 – petaire

回答

3

更改這行代碼:

let layout = UICollectionViewLayout() 

要:

let layout = UICollectionViewFlowLayout() 

UICollectionViewLayout是抽象類,你不能直接使用它。

+0

哦,男孩非常感謝! – petaire

+1

@TrungAnhPham很好發現。我在那看了10分鐘,並沒有看到它 – Scriptable