[英]Why is UICollectionViewCell's outlet nil in Interface Builder Xcode7.1 OS X Yosemite?
I have created a custom UICollectionViewCell in Interface Builder, When I run the project, the UICollectionViewCell's subViews are nil, subviews doesn't show. 我已经在Interface Builder中创建了一个自定义UICollectionViewCell,当我运行该项目时,UICollectionViewCell的subViews为nil,子视图不显示。
I checked on the Internet, someBody say " **I am calling 我在网上查了一下,有人说:“ **我在打电话
self.collectionView.registerClass(YNAskQuestionTextCollectionViewCell.self, forCellWithReuseIdentifier: "Cell_Ask_Qustion_text").
If you are using a storyboard you don't want to call this. 如果您使用的是情节提要,则不希望调用此脚本。 It will overwrite what you have in your storyboard.**" 它将覆盖情节提要中的内容。**”
Then I remove registerClass
. 然后我删除registerClass
。 But I get error as below 但我得到如下错误
init?(coder aDecoder: NSCoder)
of custom UICollectionViewCell , the error isfatal error: init(coder:) has not been implemented: file /Users/nongmeng/Desktop/nongjitong/nongjitong/class/home/conreoller/ask question/YNAskQuestionTextCollectionViewCell.swift, line 48
. 自定义UICollectionViewCell的init?(coder aDecoder: NSCoder)
,错误是fatal error: init(coder:) has not been implemented: file /Users/nongmeng/Desktop/nongjitong/nongjitong/class/home/conreoller/ask question/YNAskQuestionTextCollectionViewCell.swift, line 48
。
If I use code to add subViews, they show,Everything works fine. 如果我使用代码添加子视图,它们将显示一切正常。 I wnat know Why.Why are subViews Of UICollectionViewCell in Interface Builder nil. 我知道为什么。为什么在Interface Builder中UICollectionViewCell的子视图为零。 Anyone can help me? 有人可以帮助我吗? Here is the example project. 这是示例项目。 https://github.com/chengyanan/UICollectionView https://github.com/chengyanan/UICollectionView
This is viewController 这是viewController
//MARK: life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.registerClass(YNAskQuestionTextCollectionViewCell.self, forCellWithReuseIdentifier: "Cell_Ask_Qustion_text")
self.collectionView.registerClass(YNAskQuestionImageCollectionViewCell.self , forCellWithReuseIdentifier: "Cell_Ask_Qustion_Image")
self.collectionView.registerClass(YNAskQuestionLocationCollectionViewCell.self , forCellWithReuseIdentifier: "Cell_Ask_Qustion_Location")
let flow = UICollectionViewFlowLayout()
flow.minimumInteritemSpacing = 6
flow.minimumLineSpacing = 16
let size = CGSizeMake(self.view.frame.size.width, 100)
print(size)
flow.itemSize = size
flow.sectionInset = UIEdgeInsetsMake(0, 0, 30, 0)
flow.scrollDirection = UICollectionViewScrollDirection.Vertical
self.collectionView.collectionViewLayout = flow
self.collectionView.backgroundColor = kRGBA(234, g: 234, b: 234, a: 1)
self.collectionView.bounces = true
}
//MARK:UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let identify = "Cell_Ask_Qustion_text"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identify, forIndexPath: indexPath) as! YNAskQuestionTextCollectionViewCell
return cell
} else if indexPath.section == 2 {
let identify = "Cell_Ask_Qustion_Location"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identify, forIndexPath: indexPath) as! YNAskQuestionLocationCollectionViewCell
return cell
}
let identify = "Cell_Ask_Qustion_Image"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identify, forIndexPath: indexPath) as! YNAskQuestionImageCollectionViewCell
return cell
}
this is custom UICollectionViewCell 这是自定义UICollectionViewCell
import UIKit
class YNAskQuestionTextCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var textView: UITextView!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.greenColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
If you are using a nib file then you need to register nib file. 如果使用的是nib文件,则需要注册nib文件。 check the below code 检查以下代码
Change your code for registerClass
to registerNib
将您的registerClass
代码更改为registerNib
self.collectionView?.registerNib(UINib(nibName: "yourcellclass", bundle: nil), forCellWithReuseIdentifier: "identifier")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.