【发布时间】: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