【问题标题】:How to reload collection view inside UICollectionReusableView in swift如何在 swift 中重新加载 UICollectionReusableView 中的集合视图
【发布时间】:2017-08-10 07:52:20
【问题描述】:
  1. 我在 View 控制器中有一个 UICollectionView(命名为 aCollection),我使用 UICollectionReusableView 在集合视图顶部显示标题

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

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

    self.aCollection.reloadData()

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

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

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

    界面截图

    编辑:

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

    【讨论】:

    • 感谢您的回答!我会检查并接受它是否有效
    • 当然..:) 如果您遇到任何问题,请告诉我。
    • 当我试图重新加载 bCollection 查看它给出一个错误致命错误:在展开可选值时意外发现 nil 我的重新加载方法:ReusableView().bCollectionView.reloadData()
    • 在 UIViewController 中。得到 API 的响应后
    【解决方案2】:

    由于您的 UICollectionView (bCollection) 在 UICollectionReusableView 内,因此您只能将插座连接到 UICollectionReusableView 的类。所以我相信您可能必须为 UICollectionReusableView 创建一个自定义类并将其分配到情节提要中并将 bCollection 的出口连接到该自定义类

    【讨论】:

    • 嗨,我和你提到的一样,但它不起作用
    • @MuseerAnsari 我只是尝试了同样的方法,我能够将插座连接到 UICollectionReusableView 的自定义类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多