设置collectionView大小。 (sizeForItemAtIndexPath函数不起作用)Swift 3

问题描述 投票:22回答:5

我有一个tableView,每个tableViewCell都是collectionView

我试图用这段代码更改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)
    }

}

问题是没有错误但细胞不会改变大小

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

谢谢。

ios swift uicollectionview tableview
5个回答
59
投票

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

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

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

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

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

class MyViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

    // your code here

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

请参阅this link以了解有关Swift 3更改的更多信息。


17
投票

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

class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout

3
投票

精简版:

尝试添加

collectionView.delegate = dataSource

更长的版本:

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

collectionView.dataSource = self

这会称之为这些方法

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

但它不会打电话

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

因为这不是UICollectionView的dataSource的一部分。它是UICollectionView委托的一部分。

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

collectionView.delegate = dataSource

2
投票

所以这是我现在的代码,它的工作原理如下:

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

0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.