ios 设置集合视图大小。(sizeForItemAtIndexPath 函数不起作用)Swift 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40019875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:34:36  来源:igfitidea点击:

Set collectionView size. (sizeForItemAtIndexPath function is not working) Swift 3

iosswiftuicollectionviewtableview

提问by 0ndre_

I have a tableViewand in every tableViewCellis a collectionView.

我有一个tableView并且在每个tableViewCell是一个collectionView.

I am trying to change the collectionView size with this code:

我正在尝试使用以下代码更改 collectionView 大小:

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

    if collectionView.tag == 2{

        return CGSize(width: 100, height: 100)

    }else if collectionView.tag == 4 {


        return CGSize(width: 222, height: 200)

    }else{


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

}

The problem is that there is no error but cells do not change sizes

问题是没有错误但单元格不会改变大小

If you need to know something more, just write a comment.

如果您需要了解更多信息,请写评论。

Thank you.

谢谢你。

采纳答案by 0ndre_

So This is my code right now and it works:

所以这是我现在的代码,它可以工作:

        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.tag = row
        collectionView.setContentOffset(collectionView.contentOffset, animated:false)
        collectionView.reloadData()
        collectionView.register(UINib(nibName: "eventsCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "eventsCollectionViewCell")


        if row == 2{


            let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            layout.itemSize = CGSize(width: 120, height: 120)
            layout.scrollDirection = .horizontal

            collectionView.collectionViewLayout = layout

        }else if row == 4 {


            let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
            layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            layout.itemSize = CGSize(width: 222, height: 200)
            layout.scrollDirection = .horizontal

            collectionView.collectionViewLayout = layout

        }else{

            print("else")
        }

回答by Burak Dizlek

First, don't forget extends from UICollectionViewDelegateFlowLayoutdelegate.

首先,不要忘记从UICollectionViewDelegateFlowLayout委托扩展。

For single collectionview display logic :

对于单个 collectionview 显示逻辑:

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = ((collectionView.frame.width - 15) / 2) // 15 because of paddings
    print("cell width : \(width)")
    return CGSize(width: width, height: 200)
}

And important point in storyboard don't forget Estimate Size : None

故事板中的重点不要忘记估计大小:无

estimate_size_none

estimate_size_none

回答by Pedro Mancheno

The signature for this method has changed in Swift 3 to:

此方法的签名在 Swift 3 中已更改为:

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

Pay attention to the subtle difference: (_ collectionView: ...instead of (collectionView: ....

注意细微的差别:(_ collectionView: ...而不是(collectionView: ...

This is because in Swift 3, you have to explicitly specify the label of the first parameter. You can override this default behaviour by adding the underscore character _.

这是因为在 Swift 3 中,您必须明确指定第一个参数的标签。您可以通过添加下划线字符来覆盖此默认行为_

Also, make sure that your class adopts both UICollectionViewDelegateand UICollectionViewDelegateFlowLayoutprotocols

另外,请确保您的班级同时采用UICollectionViewDelegateUICollectionViewDelegateFlowLayout协议

class MyViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

    // your code here

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

Refer to this linkto learn more about changes in Swift 3.

请参阅此链接以了解有关 Swift 3 更改的更多信息。

回答by Vikas Krishnan

You need to specify that you implement the protocol UICollectionViewDelegateFlowLayout in your class declaration.

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

class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout

回答by BLC

Short Version:

短版

Try adding

尝试添加

collectionView.delegate = dataSource

Longer version:

更长的版本

For Me this was a small mistake -

对我来说,这是一个小错误——

collectionView.dataSource = self

This would call these methods

这将调用这些方法

func numberOfSections(in collectionView: UICollectionView) -> Int
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

However it would NOTcall

但是它不会调用

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

Because this is not part of UICollectionView 's dataSource. It's part of UICollectionView's delegate.

因为这不是 UICollectionView 的数据源的一部分。它是 UICollectionView 委托的一部分。

So adding the following solved my issue.

所以添加以下内容解决了我的问题。

collectionView.delegate = dataSource

回答by t.berlin

I've had the same problem! If you make the size half the width (width/2), then it will still appear like the entire width because the padding is being added to the width/2.

我遇到了同样的问题!如果您将尺寸设为宽度的一半 (width/2),那么它仍然会显示为整个宽度,因为填充被添加到宽度/2。

Fix: Make sure to set the section insets in Storyboard to 0. Alternatively, adjust the width/2 to a size that INCLUDES the section insets.

修复:确保将 Storyboard 中的 section insets 设置为 0。或者,将 width/2 调整为包含部分 insets 的大小。

It took me a while to fix that one ;-)

我花了一段时间来修复那个;-)

回答by Valeria

Don't forget to assign UICollectionViewFlowLayoutobject to your collectionViewLayoutsince UICollectionView uses UICollectionViewLayout (not UICollectionViewFlowLayout) by default.

不要忘记将UICollectionViewFlowLayout对象分配给collectionViewLayout,因为 UICollectionView 默认使用 UICollectionViewLayout(而不是 UICollectionView FlowLayout)。

let flowLayout = UICollectionViewFlowLayout()

collectionView.collectionViewLayout = flowLayout

让 flowLayout = UICollectionViewFlowLayout()

collectionView.collectionViewLayout = flowLayout