3

cellForItemAt indexPathUICollectionViewDelegateFlowLayout을 사용하는 경우 전화를 걸지 않습니다. 이 경우cellForItemAt UICollectionViewDelegateFlowLayout을 사용하는 경우 IndexPath가 호출하지 않습니다. iOS의 버그입니까?

:

class Something: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 

} 

가 내 cellforItemAtIndexPath를 호출하지만 호출하지 않습니다 내 내가 UICollectionViewDelegateFlowLayout을 포함하는 경우

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

하지만 :

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()해야한다. 나는 두 번 이상처럼 이것에 갇혔다.