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

标签 ios swift uicollectionview

  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/

相关文章:

ios - 符合静态方法协议(protocol)的纯 Swift 类 - 向上转换问题

swift - 如何设置firebase作为服务器并执行 strip 支付

ios - 滚动时 CollectionView 滞后

ios - 如果没有图像,则缩小 collectionView Cell 的高度

ios - 类消息的接收方“ViewController”是appdelegate类中的前向声明错误

ios - 如何将 UIBezierPath 矩形的起点和终点更改为 CAShapeLayer

objective-c - 除非单击,否则 UIButton 标题不会出现

ios - 从两个不同的 nibs Swift 附加 tableview 自定义单元格

swift - 如何将嵌入式 json 分配给 Swift 中的变量?

ios - 如何在TableView中的CollectionView中获取图像的索引路径?