3

cellForItemAt indexPathUICollectionViewDelegateFlowLayoutを使用している場合は電話がかかりません。この場合cellForItemAt私がUICollectionViewDelegateFlowLayoutを使用している場合、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

try class Something:UIViewController、UICollectionViewDelegateFlowLayout、UICollectionViewDelegate、UICollectionViewDataSource。 –

+0

私はすでにそれらを使用しています、私はちょうど私のコードの外観を短くするためにそれらを含めていませんでした@RamkrishnaSharma –

答えて

0

このコードを試してください。私にとって

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

0

、レイアウトとcollectionViewを作成し、それがlet layout = UICollectionViewFlowLayout()、無視しないととても簡単ですlet layout = UICollectionViewLayout()でなければなりません。私はこれ以上2回以上のように閉じ込められました。

関連する問題