我在 View Controller 中有一个 UICollectionView(命名为 aCollection),我使用 UICollectionReusableView 在 Collection View 的顶部显示标题
我在 UICollectionReusableView 中还有另一个 UICollectionView(名称为 bCollection)。我需要在这里显示顶级用户列表。但是当我尝试从 Storyboard连接 socket 时出现错误
我知道如何在 Collection View 中重新加载数据
self.aCollection.reloadData()
我的问题是如何连接 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)
}
}
界面截图
编辑:
重新加载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/