首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么UICollectionViewCell在接口生成器Xcode7.1 OS X Yosemite中的输出为零?

为什么UICollectionViewCell在接口生成器Xcode7.1 OS X Yosemite中的输出为零?
EN

Stack Overflow用户
提问于 2015-11-02 03:52:25
回答 1查看 1.6K关注 0票数 2

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

我在网上查了一下,someBody说:“**我正在打电话

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

如果您使用的是一个故事板,您不想称之为这。它会覆盖你在故事板上的内容。**“

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

init?(coder aDecoder: NSCoder)的自定义UICollectionViewCell,错误是fatal error: init(coder:) has not been implemented: file /Users/nongmeng/Desktop/nongjitong/nongjitong/class/home/conreoller/ask question/YNAskQuestionTextCollectionViewCell.swift, line 48

如果我使用代码添加subViews,他们会发现,一切都很好。我知道Why.Why是接口生成器中的subViews Of UICollectionViewCell。有人能帮我吗?下面是一个示例项目。https://github.com/chengyanan/UICollectionView

我是viewController

代码语言:javascript
复制
    //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

代码语言:javascript
复制
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")
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-09 06:38:44

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

registerClass代码更改为registerNib

代码语言:javascript
复制
 self.collectionView?.registerNib(UINib(nibName: "yourcellclass", bundle: nil), forCellWithReuseIdentifier: "identifier")
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33470478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档