2016-10-20 37 views
-2

我試圖做所有其他問題似乎都做的事情,但仍然出現錯誤。也許我錯過了一些明顯的東西,但我查看了文檔,看起來你需要一個佈局來初始化。即使在初始化ViewDidLoad後,UICollectionView也必須初始化爲非零布局參數

let flowLayout = UICollectionViewFlowLayout() 
flowLayout.scrollDirection = .vertical 
collectionView = UICollectionView(frame: view.frame, collectionViewLayout: flowLayout) 
collectionView?.delegate = self 
collectionView?.dataSource = self 
collectionView?.register(InterestCollectionViewCell.self, forCellWithReuseIdentifier: interestReuseIdentifier) 
view.addSubview(collectionView!) 

集合視圖的委託方法

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return interests.count 
} 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let vc = collectionView.dequeueReusableCell(withReuseIdentifier: interestReuseIdentifier, for: indexPath) as! InterestCollectionViewCell 
    vc.interestLabel.text = interests[indexPath.row].rawValue as String 
    return vc 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: view.frame.size.width, height: 45) 
} 
+2

顯示你的collectionview代表方法 –

+0

看來你有代表問題。使用此UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout – Joe

+0

更新您的委託您從代碼中缺少佈局項目大小.... layout.itemSize = CGSize(寬度:100,高度:100) – Joe

回答

0

的解決方案是,我不得不呈現UICollectionViewController之前初始化的CollectionView。我必須在viewWillAppear中註冊我的單元類,而不是viewDidLoad。

+4

你能告訴我你的代碼明智麼 –

+4

這很棒,你回答了你自己的問題。但請編輯以顯示已更改的代碼。 – MwcsMac

相關問題