3

cellForItemAt indexPath n'appelle pas si j'utilise UICollectionViewDelegateFlowLayout.cellForItemAt IndexPath n'appelle pas si j'utilise UICollectionViewDelegateFlowLayout. Est-ce un bug d'iOS?

Dans ce cas:

class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

} 

il appelle mon cellforItemAtIndexPath, mais ne remet pas mon

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

mais si j'inclus UICollectionViewDelegateFlowLayout:

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

il appellera:

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

mais n'appelle pas cellForItemAtIndexPath. Je ne comprends pas le problème. Est-ce un bug d'iOS ou je ne vois rien?

+0

Votre classe n'est pas conforme aux 'UICollectionViewDataSource' et' UICollectionViewDelegate', bien que les fonctions soient toujours appelées. De plus, votre 'cellForRow' ne sera pas appelé si votre' numberOfItems' renvoie 0. – Rikh

+0

try class Quelque chose: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource. –

+0

Je les utilise déjà, je ne les ai pas inclus pour faire paraître mon code court :) @RamkrishnaSharma –

Répondre

0

Essayez ce code.

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

Fonctionne pour moi n'est pas une réponse. – shallowThought

0

Pour moi, lors de la création du collectionView avec la mise en page, il devrait être let layout = UICollectionViewFlowLayout(), non let layout = UICollectionViewLayout() qui est si facile à négliger. J'ai été pris au piège dans cela comme plus de deux fois.