【发布时间】:2016-02-26 04:29:51
【问题描述】:
我想显示尽可能多的collectionViewCells 和buttons,因为我的数组中有字符串。但是当我启动模拟器时,只有CollectionView的背景,但没有显示单元格。可能是什么错误?
这是我的 CollectionViewController 中的代码,我附加到 main.storyboard 中的 CollectionView:
class CollectionViewController: UICollectionViewController {
var Array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Array = ["1","2","3","4","5"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int) -> Int {
return Array.count
}
override func
collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell
var button = cell.viewWithTag(1) as! UIButton
button.titleLabel?.text = Array[indexPath.row]
return cell
}
}
这些是集合视图控制器的连接:
情节提要上的视图控制器:
【问题讨论】:
-
如果您使用情节提要,这可能是一个自动布局问题。你能在 storyboard 上显示视图控制器的屏幕截图以及布局选项的屏幕截图吗?
-
我尝试禁用自动布局,但仍然存在同样的问题。布局选项是什么意思?我在情节提要上添加了视图控制器的屏幕截图
-
您是否记得将 StoryBoard 中控制器的自定义类设置为您的 CollectionViewController?
-
您是否检查过您的“func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int)”是否正在执行?
-
您的 UIViewController 是否在 UITabBarController 中?您能否检查一下这个问题是否与您的问题有关:stackoverflow.com/q/33177968/1171404
标签: ios xcode swift uicollectionviewcell collectionview