繁体   English   中英

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

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

我有一个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)
    }

}

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

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

谢谢。

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

对于单个 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)
}

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

estimate_size_none

此方法的签名在 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
    }
}

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

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

class PhrasesCompactCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout

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

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

短版

尝试添加

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 的数据源的一部分。 它是 UICollectionView 委托的一部分。

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

collectionView.delegate = dataSource

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

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

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

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

让 flowLayout = UICollectionViewFlowLayout()

collectionView.collectionViewLayout = flowLayout

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM