[英]How to use custom UICollectionReusableView as section header of collection view?
[英]How to reload collection view inside UICollectionReusableView in swift
要使用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)
}
}
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()
由于您的UICollectionView
( bCollection
)在UICollectionReusableView
内部, UICollectionReusableView
您只能将插座连接到UICollectionReusableView
的类。 因此,我相信您可能必须为UICollectionReusableView
创建一个自定义类并将其分配到情节bCollection
中,然后将bCollection
的输出连接到该自定义类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.