为什么我的“收藏夹视图单元格"不能显示在iPhone Simulator中? [英] Why won't my Collection View Cells display in the iPhone Simulator?

查看:68
本文介绍了为什么我的“收藏夹视图单元格"不能显示在iPhone Simulator中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式创建具有六个单元格的收藏夹视图". Xcode 8中的iPhone Simulator告诉我,我的构建成功. 但是仍然存在错误.

I am trying to programmatically create a Collection View with six cells. The iPhone Simulator in Xcode 8 told me that my build was successful. There is still an error though.

任何帮助发现一个或多个导致我的单元格不显示在模拟器中的错误的功能,我们将不胜感激. (我将集合视图直接从对象库放到IB中.而且,我希望每个单元格都占据集合视图的整个大小,因此为displayedCellDimensions,因为当向前或向后按钮为时,用户会在单元格之间进行选择点击.)谢谢!

Any help spotting the bug or bugs that are causing my cells not to display in the Simulator is greatly appreciated. (I put the Collection View directly into the IB from the Object Library. Also, I want each cell to occupy the entire size of the Collection View, hence displayedCellDimensions, because the user will segue between cells when a forward or backward button is tapped.) Thank you!

import UIKit

class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {



//MARK: - Collection View Properties
@IBOutlet weak var ibCollectionView: UICollectionView!

var cellArray:[customCollectionViewCell] = Array(repeating: customCollectionViewCell(), count: 6)

let displayedCellDimensions = CGSize(width: 343, height: 248)



//MARK: - Xcode-generated Methods
override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.
    ibCollectionView.dataSource = self
    ibCollectionView.delegate = self


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//MARK: - Collection View Methods
//Place cells in collection view and change the cell size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if collectionView.visibleCells.isEmpty {
        for cell in 0...cellArray.count {
            collectionView.addSubview(cellArray[cell])
            return CGSize(width: 343, height: 248)
        }
    }
    return CGSize(width: 343, height: 248)
}

//Register a new cell
func registerCell(_ collectionView: UICollectionView) {
    for _ in 0...cellArray.count {
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    }
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
    return cell
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return cellArray.count


}
...}

推荐答案

在您的viewDidLoad()中尝试:

In your viewDidLoad() Try:

viewDidLoad() {
ibCollectionView.dataSource = self
ibCollectionView.delegate = self
}

将其添加到班级顶部:

class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

您必须在ViewController类中使用以下功能:

You must use these functions in your ViewController class:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}

这篇关于为什么我的“收藏夹视图单元格"不能显示在iPhone Simulator中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆