这是我对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
}
问题是每当我尝试运行应用程序时,在调试之后它总是弹出错误"致命错误:在展开可选值时意外发现nil"在行
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!)
}
答案 0 :(得分:0)
我发现在我的Header.swift类中,我不小心添加了不存在的图像名称
module PgTags
def tags=(value)
write_attribute :tags, value.uniq
end
end
class Rule < ActiveRecord::Base
include PgTags
end
r = Rule.new
r.tags = %w(one two one)
puts r.tags # outputs ['one', 'two']
r.tags << 'one'
puts r.tags # should still output ['one', 'two']
所以我删除了图片名称,所有这些都恢复正常:)