如何使用swift在集合视图中创build页眉和页脚
如何使swift中的集合视图中的页眉和页脚?
我试图将一个页眉和一个页脚结合在一起,但它不断崩溃,我找不到快速教程来理解它。
我不知道如何为两者返回补充观点。
我把它们都放在故事板上(class +标识符)
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { //#warning Incomplete method implementation -- Return the number of sections return 2 } override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //#warning Incomplete method implementation -- Return the number of items in the section return 10 } override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { var header: headerCell! var footer: footerCell! if kind == UICollectionElementKindSectionHeader { header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as? headerCell } return header }
错误:标识符为1的UICollectionElementKindCell – 必须为标识符注册一个笔尖或类,或者在故事板中连接一个原型单元格“
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> profileCC { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("one", forIndexPath: indexPath) as! profileCC // Configure the cell return cell } override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! headerCell headerView.backgroundColor = UIColor.blueColor(); return headerView case UICollectionElementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "footer", forIndexPath: indexPath) as! footerCell footerView.backgroundColor = UIColor.greenColor(); return footerView default: assert(false, "Unexpected element kind") } }
我希望有人会帮助。
您可以使UICollectionViewController
处理UICollectionView
并在Interface Builder中激活页脚和页眉部分,然后您可以使用以下方法在您的UICollectionView
预览添加的两个部分:
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Header", forIndexPath: indexPath) as! UICollectionReusableView headerView.backgroundColor = UIColor.blueColor(); return headerView case UICollectionElementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "Footer", forIndexPath: indexPath) as! UICollectionReusableView footerView.backgroundColor = UIColor.greenColor(); return footerView default: assert(false, "Unexpected element kind") } }
在上面的代码中,我将Header
和Header
的identifier
作为Header
和Footer
,例如,您可以按照自己的意愿进行操作。 如果你想创build一个自定义的页眉或页脚,那么你需要为每个创build一个UICollectionReusableView
的子类,并根据需要进行自定义。
您可以通过以下方式在Interface Builder或代码中注册自定义页脚和标题类:
registerClass(myFooterViewClass, forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "myFooterView")
更新了Swift 3.0
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { switch kind { case UICollectionElementKindSectionHeader: let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) headerView.backgroundColor = UIColor.blue return headerView case UICollectionElementKindSectionFooter: let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Footer", for: indexPath) footerView.backgroundColor = UIColor.green return footerView default: assert(false, "Unexpected element kind") } }
和:
self.collectionView.register(MyClass.self, forCellWithReuseIdentifier: "MyClass")
- 在Swift中,$ 0代表什么?
- UIAlertcontroller在Swift中显示时保持键盘是否打开?
- UIAlertControllerbuttonfunction不起作用
- Alamofire Image:在af_setImageWithURL()之后从AutoPurgingImageCache获取图像
- iOSmultithreading – NSURLSession和UI更新
- 内存泄漏:通过简单的设备运动logging稳定增加内存使用量
- CAShapeLayer在animationSwift中检测触摸
- 用CFStringTransform和Swift 3模糊地使用'init'
- Swift – 有一个audio重复,但它会间隔?