为啥未显示集合可重用视图?
Posted
技术标签:
【中文标题】为啥未显示集合可重用视图?【英文标题】:Why Collection Reusable View not shown?为什么未显示集合可重用视图? 【发布时间】:2017-05-16 15:13:49 【问题描述】:我想创建带有标题的 UICollectionView。我在 mainStoryBoard 上设置了 Collection Reusable View,但设备上没有显示任何内容。我试图搜索但找不到它为什么没有出现。我
主要故事板
在设备上
导入 UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
@IBOutlet weak var collectionView: UICollectionView!
var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"]
var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"]
override func viewDidLoad()
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return images.count
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.myImage.image = UIImage(named: images[indexPath.row])
cell.achievementLabel.text = texts[indexPath.row]
return cell
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
return 1
import UIKit
集合视图类 类 CustomCell: UICollectionViewCell
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var achievementLabel: UILabel!
集合可重用视图的类 导入 UIKit
class CollectionReusableView: UICollectionReusableView
@IBOutlet weak var reuseableVimage: UIImageView!
> import UIKit
class ViewController: UIViewController, UICollectionViewDelegate
@IBOutlet weak var collectionView: UICollectionView!
var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"]
var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"]
override func viewDidLoad()
super.viewDidLoad()
collectionView.delegate = self
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
if kind == UICollectionElementKindSectionHeader
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexPath)
// do any programmatic customization, if any, here
return view
fatalError("Unexpected kind")
【问题讨论】:
【参考方案1】:你必须实现viewForSupplementaryElementOfKind
:
针对UICollectionElementKindSectionHeader
或UICollectionElementKindSectionFooter
酌情实施collectionView(_:viewForSupplementaryElementOfKind:at:)
。
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
if kind == UICollectionElementKindSectionHeader
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CollectionReusableView", for: indexPath) as! CollectionReusableView
// do any programmatic customization, if any, here
return view
fatalError("Unexpected kind")
确保 IB 中的标题可重用视图具有
适当的基类;和 适当的“重用标识符”在 IB 中,确保集合视图的“附件”在“Section Header”和“Section Footer”旁边有复选标记。
【讨论】:
谢谢。我更新了代码,但现在屏幕什么也没显示。我检查了 IB 和附件,一切都很好。 假设没有警告,我可能会检查视图调试器,看看集合视图中是否有视图以及它们的框架是什么。如果真的没有视图,那么我会在UICollectionViewDataSource
方法中设置断点并查看它们是否被调用以及它们返回的值。
当我设置 collectionView.dataSource = self 时,它会迷恋。
这是断点 ""let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CollectionReusableView", for: indexPath) as! CollectionReusableView""
听起来像是标题视图没有匹配的“重用标识符”或标题的基类尚未设置。【参考方案2】:
尝试实现这一点。 RayWenderlich 中有一个可能对您有用的示例:https://www.raywenderlich.com/136161/uicollectionview-tutorial-reusable-views-selection-reordering
override func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView
switch kind
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: "CollectionReusableView",
for: indexPath) as! CollectionReusableView
headerView.reuseableVimage ....
return headerView
default:
assert(false, "Unexpected element kind")
【讨论】:
看来swift 3有一些变化:/以上是关于为啥未显示集合可重用视图?的主要内容,如果未能解决你的问题,请参考以下文章
使用带标识符的双端可重用单元格获取 UIView 时,UIFooterView 未显示
UITableView 可重用的自定义单元格在子视图中显示错误的内容
显示 SVG 图标的 Angular 可重用组件的宽度/高度未在任何地方设置并位于图标正下方,stackblitz 内部