所以设置肯定是相关的。显然ib在为你的uicollectionview加载单元格时崩溃了。它无法加载nib文件。它与追加没有任何关系,只是同时崩溃了。我复制了你的问题,然后用代码创建了一个单元格,一切正常。如果这有帮助,请告诉我,并回答您的问题。所有必需的代码都在那里。
import UIKit
struct HashTag {
var word:String?
}
class HashCollectionViewCell: UICollectionViewCell {
lazy var wordLabel : UILabel = {
let lbl = UILabel(frame: self.bounds)
lbl.font = UIFont.systemFont(ofSize: 17)
lbl.textColor = .black
return lbl
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(wordLabel)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.addSubview(wordLabel)
}
func configureWithTag(tag:HashTag) {
wordLabel.text = tag.word
}
}
@IBDesignable
class HashTagView: UIView {
private var sizingLabel = UILabel(frame: .zero)
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let view = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
view.autoresizingMask = [.flexibleWidth,.flexibleHeight]
return view
}()
var hashtags: [HashTag] = [HashTag(word: "this works")]
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
sizingLabel.font = UIFont.systemFont(ofSize: 17)
self.addSubview(collectionView)
collectionView.frame = self.bounds
collectionView.backgroundColor = self.backgroundColor
collectionView.register(HashCollectionViewCell.self, forCellWithReuseIdentifier: "hashCell")
self.addSubview(collectionView)
for x in 0..<10{
addHashTag(tag: "This is more \(x)")
}
collectionView.delegate = self
collectionView.dataSource = self
}
func addHashTag(tag:String){
let newHash = HashTag(word: tag)
self.hashtags.append(newHash)
}
override func layoutSubviews() {
super.layoutSubviews()
collectionView.frame = self.bounds
}
}
extension HashTagView: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return hashtags.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "hashCell", for: indexPath) as? HashCollectionViewCell{
let tag = self.hashtags[indexPath.item]
cell.configureWithTag(tag: tag)
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let tag = self.hashtags[indexPath.item]
sizingLabel.text = tag.word
sizingLabel.sizeToFit()
return sizingLabel.frame.size
}
}
结果:
2)您可以将nib加载到以编程方式创建的单元中,它将工作。
对于编程单元这一部分,一个示例将如下所示。
使用单元内部NIB扩展视图的单元代码:
extension UIView {
func loadNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nibName = type(of: self).description().components(separatedBy: ".").last!
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as! UIView
}
}
class HashCollectionViewCell: UICollectionViewCell {
var hashTagNib : HashTagNibView?
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup(){
if let htn = HashTagNibView().loadNib() as? HashTagNibView{
hashTagNib = htn
hashTagNib?.frame = self.bounds
hashTagNib?.autoresizingMask = [.flexibleWidth,.flexibleHeight]
self.addSubview(htn)
}
}
func configureWithTag(tag:HashTag) {
if let htn = hashTagNib{
htn.hashTagButtonLabel.setTitle(tag.word, for: .normal)
}
}
NIB设置如下:
这可能会给你更多的灵活性,这取决于你在做什么,结果是相似的,除了在nib的例子中,我使用了一个uibutton。