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

cellForItemAt如果我正在使用UICollectionViewDelegateFlowLayout,则不会调用indexPath.

在这种情况下:

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的错误还是我看不到任何东西?

最佳答案 试试这个代码.

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)
    }

}
点赞