iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,UICollectionViewDatasource,UICollectionViewDelegateFlowLayout {
}

它会打电话给:

func collectionView(_ collectionView: UICollectionView,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,sizeForItemAt indexPath: IndexPath) -> CGSize {

        print("Here sizeForItemAt Works")

        return CGSize(width: 150,height: 150)
    }

}

大佬总结

以上是大佬教程为你收集整理的cellForItemAt如果我正在使用UICollectionViewDelegateFlowLayout,则不会调用IndexPath.这是iOS的错误吗?全部内容,希望文章能够帮你解决cellForItemAt如果我正在使用UICollectionViewDelegateFlowLayout,则不会调用IndexPath.这是iOS的错误吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。