Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView无法显示自定义单元格内容_Ios_Swift_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios UICollectionView无法显示自定义单元格内容

Ios UICollectionView无法显示自定义单元格内容,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我试图用UICollectionView创建一个虚拟照片应用程序。我的Xcode版本是7.2和Swift版本2.1.1。我使用以下教程来构建UI部件: 在Swift 2.0中,UICollectionViewDataSource继承自UICollectionViewController,我们不需要显式声明这些协议。我为DataSource实现了所需的覆盖方法,并在UICollectionViewController中的viewDidLoad()中注册了自定义单元格。我在我的手机里放了一个测试标签

我试图用UICollectionView创建一个虚拟照片应用程序。我的Xcode版本是7.2和Swift版本2.1.1。我使用以下教程来构建UI部件:

在Swift 2.0中,UICollectionViewDataSource继承自UICollectionViewController,我们不需要显式声明这些协议。我为DataSource实现了所需的覆盖方法,并在UICollectionViewController中的viewDidLoad()中注册了自定义单元格。我在我的手机里放了一个测试标签来检查它是否有效。不幸的是,在我启动应用程序后,标签从未出现。我附上我的一些代码作为参考:

UICollectionViewController

class VacationsCollectionViewController: UICollectionViewController {

    private let reuseIdentifier = "VacationCell"

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(VacationsCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 1
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! VacationsCollectionViewCell

        // Configure the cell
        cell.backgroundColor = UIColor.blueColor()
        cell.CellText.text = "Show me the money"

        return cell
    }
}
UICollectionViewCell

class VacationsCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var CellText: UILabel!
}

你知道为什么这个标签从来没有出现在我的虚拟应用程序中吗?

看起来你拥有你需要的一切。问题可能在于你的故事板。为了安全起见,在标签上添加约束,这样它肯定会正确显示。然后,向集合视图控制器添加一个扩展,并确保其大小也允许显示标签。至少,这对我是有效的,我正处在你的处境

extension OfficesCollectionViewController: UICollectionViewDelegateFlowLayout{
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSize(width: 200, height: 200)
    }
}

在情节提要中,选择
UICollectionViewCell
并打开属性检查器。确保您在字段标识符中输入了
VacationCell
。@dfri是的,标识符的值为
VacationCell
。不过,感谢您的提醒,这很容易忘记。我最终决定使用完全相同的逻辑切换到UITableView,这很有效。谢谢你的回答。