集合视图单元格未出现在集合视图中?
Collection View Cells not appearing in Collection View?
我纯粹使用故事板界面生成器创建了一个集合视图。这是故事板的样子:
我的集合视图的属性也是默认的。我还没有在 ViewController.swift
中写入任何内容。
出于某种原因,当我在 phone / 模拟器上 运行 时,显示 none 个按钮。
UICollectionView 不支持像 UITableView 这样的静态单元格。您必须在代码中设置其数据源、委托和配置您的单元格。
只需正确配置 collectionView
参见下面的代码和图像:
实现collectionView
的delegate
方法:
class yourClassController: UICollectionViewDataSource, UICollectionViewDelegate {
override func numberOfSectionsInCollectionView(collectionView:
UICollectionView!) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView!,
numberOfItemsInSection section: Int) -> Int {
return yourArray.count
}
override func collectionView(collectionView: UICollectionView!,
cellForItemAtIndexPath indexPath: NSIndexPath!) ->
UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
cell.backgroundColor = UIColor.blackColor()
cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
cell.imageView?.image = UIImage(named: "circle")
return cell
}
然后从您的 storyboard
通过拖放设置 delegate
和 datasource
参见图片:
注: collectionView
完成以上手续后会出现class.
我纯粹使用故事板界面生成器创建了一个集合视图。这是故事板的样子:
我的集合视图的属性也是默认的。我还没有在 ViewController.swift
中写入任何内容。
出于某种原因,当我在 phone / 模拟器上 运行 时,显示 none 个按钮。
UICollectionView 不支持像 UITableView 这样的静态单元格。您必须在代码中设置其数据源、委托和配置您的单元格。
只需正确配置 collectionView
参见下面的代码和图像:
实现collectionView
的delegate
方法:
class yourClassController: UICollectionViewDataSource, UICollectionViewDelegate {
override func numberOfSectionsInCollectionView(collectionView:
UICollectionView!) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView!,
numberOfItemsInSection section: Int) -> Int {
return yourArray.count
}
override func collectionView(collectionView: UICollectionView!,
cellForItemAtIndexPath indexPath: NSIndexPath!) ->
UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
cell.backgroundColor = UIColor.blackColor()
cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
cell.imageView?.image = UIImage(named: "circle")
return cell
}
然后从您的 storyboard
通过拖放设置 delegate
和 datasource
参见图片:
注: collectionView
完成以上手续后会出现class.