gpt4 book ai didi

ios - 如何在 swift 中重新加载 UICollectionReusableView 中的 Collection View

转载 作者:行者123 更新时间:2023-11-28 15:23:23 25 4
gpt4 key购买 nike

  1. 我在 View Controller 中有一个 UICollectionView(命名为 aCollection),我使用 UICollectionReusableView 在 Collection View 的顶部显示标题

    enter image description here

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

    enter image description here

  3. 我知道如何在 Collection View 中重新加载数据

    self.aCollection.reloadData()

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

最佳答案

要取bCollection的导出,需要创建UICollectionReusableView的子类。

示例:

UIViewController 包含 aCollection:

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

UICollectionReusableView 包含 bCollection:

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

界面截图

enter image description here

编辑:

重新加载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()

关于ios - 如何在 swift 中重新加载 UICollectionReusableView 中的 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45607624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com