3

cellForItemAt indexPath не звонит, если я использую UICollectionViewDelegateFlowLayout.cellForItemAt IndexPath не вызывает, если я использую UICollectionViewDelegateFlowLayout. Это ошибка 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', хотя функции все равно должны вызываться. Кроме того, ваш 'cellForRow' не будет вызываться, если ваш' numberOfItems' возвращает 0. – 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(), который так легко пренебречь. Я был в ловушке этого, как более двух раз.

 Смежные вопросы

  • Нет связанных вопросов^_^