Ios 从未调用CollectionView sizeForItemAtIndexPath

Ios 从未调用CollectionView sizeForItemAtIndexPath,ios,Ios,这快把我逼疯了!我有一个UICollectionViewController,如下所示: class PhrasesCompactCollectionViewController: UICollectionViewController 正在调用numberOfSections和cellForItemAt,但从未调用sizeForItemAtIndexPath。我在其他地方使用了相同的代码,它正确地触发了。我使用的是Xcode 8 Beta 6 func collectionView(colle

这快把我逼疯了!我有一个UICollectionViewController,如下所示:

class PhrasesCompactCollectionViewController: UICollectionViewController
正在调用numberOfSections和cellForItemAt,但从未调用sizeForItemAtIndexPath。我在其他地方使用了相同的代码,它正确地触发了。我使用的是Xcode 8 Beta 6

func collectionView(collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAtIndexPath indexPath:  NSIndexPath) -> CGSize {

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

您需要在类声明中指定实现协议
UICollectionViewDelegateFlowLayout


类短语CompactCollectionViewController:UICollectionViewController,UICollectionViewDelegateFlowLayout

Swift 3

要设置UICollectionView的单元格大小,您需要创建并自定义
UICollectionViewFlowLayout
的对象。自定义属性并将该布局对象设置为UICollectionView对象

根据需要(所需的单元格高度和宽度)更改单元格高度和单元格宽度对象

确保:如果添加了sizeForItemAt indexPath方法,则必须删除该方法…

删除下面的方法


Swift 3+中,使用以下方法:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width:(collectionView.frame.height-90)/2, height: 100)
}
确保您的类符合两个委托

  • UICollectionViewDelegate


  • UICollectionViewDelegateFlowLayout

对于我(在Swift 3.1中),该方法必须声明为公开的,如下所示:

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

不要忘记将其公开

头等舱需要确认UICollectionViewDelegateFlowLayout。然后,您需要在viewDidLoad()中编写以下代码:


//告诉UICollectionView使用UIViewController的UICollectionViewDelegateFlowLayout方法

collectionView.delegate = self
//如果要设置自己的集合视图流布局

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //depending upon direction of collection view

self.collectionView?.setCollectionViewLayout(layout, animated: true)

通过使用此代码UICollectionViewDelegateFlowLayout方法

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

将被调用,您可以使用此方法设置大小。

我昨晚遇到此问题。终于在午夜解决了这个问题,当时我意识到“didSelectItemAtIndex”方法并没有用闭包括号“}”来关闭

当编译错误询问时,我在类的最底部添加了一个闭包括号。因此,实际上,“didSelectItemAtIndex”下面的所有方法都在该方法中


在这里发布,以防其他人在这篇文章上浪费4个小时的晚上:-(

请确保您将这篇文章放在viewDidLoad()中)


我也遇到了同样的问题,没有任何东西可以解决。我收到了一条黄色警告,说要将其设置为私有,因为它接近布局委托定义的另一个函数。为我解决了这个问题的是:

func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize
请注意“SizeFormiteIndeXPath”与正确的“SizeFormiteMat”之间的差异

我花了好几天的时间试图弄明白这一点,结果终于成功了。下面我将介绍我的所有函数,向您展示如何通过使高度等于0来“隐藏”一个单元格

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let findIndex = labelArray.index(of: "\(labelArray[indexPath.row])")
    let screenSize = UIScreen.main.bounds
    let screenWidth = screenSize.width

    if (findIndex! % 2 == 0){
        // Even index, show cell
        return CGSize(width: screenWidth, height: 246)
    }
    else {
        return CGSize(width: screenWidth, height: 0)
    }
}

我也遇到了同样的问题。帮助我的一件事是在collectionView大小检查器“None”下为单元格大小创建“估计大小”


如果没有答案,您需要检查三件事:


  • 实现FlowLayout委托:
    类MyController:UICollectionViewController,UICollectionViewDelegateFlowLayout

  • 对swift 3.0+的项目大小使用方法:
    func collectionView(\uCollectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
    返回CGSize(宽度:(collectionView.frame.width/3)-1,高度:collectionView.frame.width)
    }

  • 在UICollectionView的Inspect元素中选择
    Estimate size=None


  • 我的问题是我将UICollectionViewDataSource对象创建为局部变量。因此,如果您在代码中手动初始化数据源对象,请确保将其存储为全局变量。

    Swift 5

    所有这些步骤都是必需的:


  • 符合UICollectionViewDelegateFlowLayout的要求使用此方法

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

    private func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    
    }
    

    只需添加:UICollectionViewDelegateFlowLayout

    class YourClass: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {}
    

    如果您使用的是
    autolayout
    ,请确保设置了
    collectionView.translatesAutoresizingMaskIntoConstraints=false

    可能您设置了错误的布局:
    UICollectionViewLayout
    ,实际上此布局应该是
    UICollectionViewFlowLayout
    类似的布局布局。在xib/故事板中设置间距,并使用下面更新的功能调整单元格大小,以更新Swift版本。快乐编码

       @objc(collectionView:layout:sizeForItemAtIndexPath:)
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        let numberOfItemsPerRow:CGFloat = 5
        
        if let collection = self.collectionView {
            let width = collection.bounds.width / numberOfItemsPerRow
            
            return CGSize(width: width, height: 36)
        }else{
            return CGSize(width: 0, height: 0)
        }
    }
    

    我打电话解决了同样的问题

    collectionView.delegate = self
    
    之前

    collectionView.dataSource = self
    
    我有静态项,从不重新加载数据。
    显然,设置数据源会触发同步加载集合数据,因此如果此时未设置委托,则永远不会调用
    SizeForestimationIndeXPath
    (除非以后调用
    reloadData
    ).

    这有什么帮助?Almas,这将有助于设置UICollectionViewCell的自定义单元格大小…有变量cellWidth和cellHeight,您可以定义自己的值…以及“layout.sectionInset”、“layout.minimumLineSpacing”和“layout.MinimumItemSpacing”,这些变量对UICollectionViewCell size也很重要。这仅在所有单元格大小相同的情况下有效。下面的答案是正确的答案这是为了更改UICollectionView的大小,而不是视图中的单元格。这不是为什么不调用委托方法的答案。这是多余的,符合
    UICollectionViewDelegate
    就像UICollectionViewController已经做过的那样。@VanDuTran说它是必需的并不是多余的,因为作为UICollectionViewController不是必需的……我想你是我
    class YourClass: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {}
    
       @objc(collectionView:layout:sizeForItemAtIndexPath:)
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        let numberOfItemsPerRow:CGFloat = 5
        
        if let collection = self.collectionView {
            let width = collection.bounds.width / numberOfItemsPerRow
            
            return CGSize(width: width, height: 36)
        }else{
            return CGSize(width: 0, height: 0)
        }
    }
    
    collectionView.delegate = self
    
    collectionView.dataSource = self