【问题标题】:Why won't my Collection View Cells display in the iPhone Simulator?为什么我的收藏视图单元格不会显示在 iPhone 模拟器中?
【发布时间】:2017-08-02 14:14:30
【问题描述】:

我正在尝试以编程方式创建一个包含六个单元格的集合视图。 Xcode 8 中的 iPhone 模拟器告诉我构建成功。 仍然有错误。

非常感谢您发现导致我的单元格不显示在模拟器中的错误或错误的任何帮助。 (我将集合视图从对象库直接放入 IB。另外,我希望每个单元格占据集合视图的整个大小,因此 displayedCellDimensions,因为当前进或后退按钮时,用户将在单元格之间切换轻拍。)谢谢!

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


}
...}

【问题讨论】:

  • 阅读此raywenderlich.com/136159/… 你没有设置你的 UICollectionView
  • @TarasChernyshenko - 本教程从整个 Collection View Controller 场景开始。既然我想在这个场景中包含其他东西,这是否意味着我需要将我的 Collection View Controller 放在一个 Container View 中?
  • 教程教你如何设置和使用 UICollectionView
  • 在这个函数中返回 1 func numberOfSections(in collectionView: UICollectionView) -> Int {}

标签: swift xcode uicollectionview ios-simulator uicollectionviewcell


【解决方案1】:

在你的 viewDidLoad() 中尝试:

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

将此添加到班级顶部:

class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

您必须在 ViewController 类中使用这些函数:

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

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

【讨论】:

  • 这给出了一条错误消息,指出我的类的名称不符合 UICollectionViewDataSource 协议。不过,委托人似乎正在工作。
  • 你在 numberOfItemsInSection 中返回一个 Int...你能更新你的代码吗..@ProgrammingEnthusiast
  • 好的,我编辑了代码,我也意识到我认为有些函数可能是相互重复的。另外,我是否需要将 UICollectionView 放在容器视图中的集合视图控制器内(在主视图控制器内?
  • 不,你可以在视图控制器中拥有它。不需要在 UICollectionViewcontroller 中
  • 我在玩代码:我确实有一个重复的函数,我想我已经接近获得numberOfItemsInSection 函数的代码了。将使用结果更新代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2011-07-09
  • 1970-01-01
  • 2021-12-02
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多