Custom header "fatal error: unexpectedly found nil while unwrapping an Optional value"
Custom header "fatal error: unexpectedly found nil while unwrapping an Optional value"
这是我的 UICollectionUICollectionViewControllerView 的覆盖函数
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var view = UICollectionReusableView()
if kind == UICollectionElementKindSectionHeader {
let viewHeader : Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header
if let userName = viewHeader.userName {
userName.text = "James Bond"
}
view = viewHeader
}
return view
}
问题是每当我尝试 运行 应用程序时,在调试之后它总是在
行中弹出错误 "fatal error: unexpectedly found nil while unwrapping an Optional value"
let viewHeader : Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header
下面是我的 viewDidLoad 覆盖
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
self.collectionView!.delegate = self
collectionView!.dataSource = self
collectionView!.registerClass(SquareCell.self, forCellWithReuseIdentifier: "cell")
collectionView!.registerClass(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header") // UICollectionReusableView
layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)
let cellNib = UINib(nibName: "SquareCell", bundle: nil)
collectionView!.registerNib(cellNib, forCellWithReuseIdentifier: "cell")
let headerlNib = UINib(nibName: "HeaderView", bundle: nil)
collectionView!.registerNib(headerlNib, forCellWithReuseIdentifier: "header")
self.view.addSubview(collectionView!)
}
我发现在我的 Header.swift class 中不小心添加了 non-existing 图片名称
override init(frame: CGRect) {
super.init(frame: frame)
self.cover.image = UIImage(named: "cover")
self.userName.text = "Jame Bond"
}
所以我删除了图像名称,一切恢复正常:)
这是我的 UICollectionUICollectionViewControllerView 的覆盖函数
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
var view = UICollectionReusableView()
if kind == UICollectionElementKindSectionHeader {
let viewHeader : Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header
if let userName = viewHeader.userName {
userName.text = "James Bond"
}
view = viewHeader
}
return view
}
问题是每当我尝试 运行 应用程序时,在调试之后它总是在
行中弹出错误 "fatal error: unexpectedly found nil while unwrapping an Optional value"let viewHeader : Header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "header", forIndexPath: indexPath) as! Header
下面是我的 viewDidLoad 覆盖
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
self.collectionView!.delegate = self
collectionView!.dataSource = self
collectionView!.registerClass(SquareCell.self, forCellWithReuseIdentifier: "cell")
collectionView!.registerClass(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header") // UICollectionReusableView
layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)
let cellNib = UINib(nibName: "SquareCell", bundle: nil)
collectionView!.registerNib(cellNib, forCellWithReuseIdentifier: "cell")
let headerlNib = UINib(nibName: "HeaderView", bundle: nil)
collectionView!.registerNib(headerlNib, forCellWithReuseIdentifier: "header")
self.view.addSubview(collectionView!)
}
我发现在我的 Header.swift class 中不小心添加了 non-existing 图片名称
override init(frame: CGRect) {
super.init(frame: frame)
self.cover.image = UIImage(named: "cover")
self.userName.text = "Jame Bond"
}
所以我删除了图像名称,一切恢复正常:)