繁体   English   中英

如何在Swift中的UICollectionReusableView内部重新加载集合视图

[英]How to reload collection view inside UICollectionReusableView in swift

  1. 我在View控制器中有一个UICollectionView(让名字为aCollection),我使用UICollectionReusableView在集合视图的顶部显示标题

    在此处输入图片说明

  2. 我在UICollectionReusableView中还有另一个UICollectionView(名称为bCollection)。 我需要在这里显示顶级用户列表。 但是当我尝试从情节提要连接插座时,出现错误

    在此处输入图片说明

  3. 我知道如何在集合视图中重新加载数据

    self.aCollection.reloadData()

  4. 我的问题是如何连接bCollection插座以及如何重新加载bCollection以显示来自Web服务的用户列表?

要使用bCollection ,您需要创建UICollectionReusableView的子类。

例:

包含aCollection UIViewController

class ViewController: UIViewController, UICollectionViewDataSource
{
    @IBOutlet weak var aCollectionView: UICollectionView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "aCell", for: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
    {
        return (collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "reusableView", for: indexPath) as! ReusableView)
    }
}

UICollectionReusableViewbCollection

class ReusableView: UICollectionReusableView, UICollectionViewDataSource
{
    @IBOutlet weak var bCollectionView: UICollectionView!

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "bCell", for: indexPath)
    }
}

界面截图

在此处输入图片说明

编辑:

重新加载bCollection

您需要对正在使用的reusableView的引用。 您使用它的方式ReusableView()不正确。

像这样使用它:

var reusableView: ReusableView?

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
{
    self.reusableView = (collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "reusableView", for: indexPath) as! ReusableView) //Storing the reference to reusableView
    return self.reusableView!
}

现在,要重新加载bCollection

    self.reusableView?.bCollectionView.reloadData()

由于您的UICollectionViewbCollection )在UICollectionReusableView内部, UICollectionReusableView您只能将插座连接到UICollectionReusableView的类。 因此,我相信您可能必须为UICollectionReusableView创建一个自定义类并将其分配到情节bCollection中,然后将bCollection的输出连接到该自定义类

暂无
暂无

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

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