为什么UICollectionViewCell的出口在Interface Builder Xcode7.1 OS X Yosemite中没有?

时间:2015-11-02 03:52:25

标签: ios iphone swift uicollectionview uicollectionviewcell

我在Interface Builder中创建了一个自定义UICollectionViewCell,当我运行该项目时,UICollectionViewCell的子视图为零,子视图不显示。

我在网上查了一下,有些人说" **我打电话

self.collectionView.registerClass(YNAskQuestionTextCollectionViewCell.self, forCellWithReuseIdentifier: "Cell_Ask_Qustion_text").

如果您使用的是故事板,则不要打电话给它。它会覆盖你的故事板中的内容。**"

然后我删除registerClass。但我得到如下错误

  自定义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

如果我使用代码添加子视图,他们会显示,一切正常。我知道为什么。为什么Interface Builder中的UICollectionViewCell的子视图为零。有人可以帮帮我吗?这是示例项目。 https://github.com/chengyanan/UICollectionView

这是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
    }

这是自定义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")
    }

}

1 个答案:

答案 0 :(得分:4)

如果您使用的是nib文件,则需要注册nib文件。检查以下代码

registerClass的代码更改为registerNib

 self.collectionView?.registerNib(UINib(nibName: "yourcellclass", bundle: nil), forCellWithReuseIdentifier: "identifier")