如果我使用UICollectionViewDelegateFlowLayout,cellForItemAt IndexPath不会调用。这是iOS的错误吗?

问题描述:

cellForItemAt indexPath如果我使用UICollectionViewDelegateFlowLayout,则不会调用。如果我使用UICollectionViewDelegateFlowLayout,cellForItemAt IndexPath不会调用。这是iOS的错误吗?

在这种情况下:

class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

} 

它叫我cellforItemAtIndexPath,但不叫我的

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
} 

,但如果我有UICollectionViewDelegateFlowLayout

class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 
} 

它会调用:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
} 

但不会呼叫cellForItemAtIndexPath。我不明白这个问题。这是iOS的错误还是我没有看到任何东西?

+0

你的类不符合'UICollectionViewDataSource'和'UICollectionViewDelegate',但函数仍应该被调用。此外,如果您的'numberOfItems'返回0,您的'cellForRow'将不会被调用。 – Rikh

+0

尝试类Something:UIViewController,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource。 –

+0

我已经使用它们,我只是没有包括它们让我的代码看起来很短:) @RamkrishnaSharma –

试试这个代码。

import UIKit 

let kSCREENWIDTH = UIScreen.main.bounds.size.width 
let kSCREENHEIGHT = UIScreen.main.bounds.size.height 

let COLLECTION_CELL = "collectionCell" 

class DemoController: UIViewController { 

    var collectionView : UICollectionView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     setupCollectionView() 
    } 

    /// create programatically collection view 
    fileprivate func setupCollectionView() { 

     let layout = UICollectionViewFlowLayout() 
//  layout.itemSize = CGSize(width:(kSCREENWIDTH-20)/2,height:150) 
     layout.sectionInset = UIEdgeInsetsMake(5, 7, 5, 7) 
     layout.minimumLineSpacing = 5 
     layout.minimumInteritemSpacing = 1 

     collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout) 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.backgroundColor = UIColor.white 
     self.view .addSubview(collectionView) 

     collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: COLLECTION_CELL) 
    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 


//MARK: UICollectionViewDelegate 

extension DemoController : UICollectionViewDelegate { 

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

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
      // add your code here 
    } 

} 

//MARK: UICollectionViewDataSource 
extension DemoController : UICollectionViewDataSource { 


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     /// add your code here 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: COLLECTION_CELL, for: indexPath) 

     cell.backgroundColor = UIColor.lightGray 

     print("Here cellForItemAt Works") 

     return cell 
    } 


} 

//MARK: UICollectionViewDelegateFlowLayout 
extension DemoController : UICollectionViewDelegateFlowLayout { 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     print("Here sizeForItemAt Works") 

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

} 
+2

适合我的作品不是答案。 – shallowThought

对于我来说,创建collectionView与布局时,它应该是let layout = UICollectionViewFlowLayout(),不let layout = UICollectionViewLayout()这是很容易忽视。我被困在这里不止两次。