Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 向CollectionViewCell添加标题_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 向CollectionViewCell添加标题

Ios 向CollectionViewCell添加标题,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我在表视图中有一个集合视图。接下来我要做的是为集合视图中的每个单元格设置一个标题 我在ViewController中有这个代码 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentif

我在表视图中有一个集合视图。接下来我要做的是为集合视图中的每个单元格设置一个标题

我在ViewController中有这个代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
   cell.titleLabel.text = "123"
   return cell;
}
这是细胞类

class CollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var titleLabel: UILabel!

}
当我尝试在单元格内添加标签,然后使用此标签设置标题时,单元格内不会显示任何内容

这是我的当前堆栈:

我做错了什么


单元格的标题是静态的还是动态的?如果它们是静态的,我不会使用
UICollectionViewDataSource
,而是只使用interface builder单元格。如果标题是动态的,即取决于某种类型的用户输入/每个设备或每个外观的不同,则您需要确保在
viewDidLoad
中设置
collectionView.dataSource=self
。当然,这需要您引用一些对象数组,这些对象将指定每个单元格的标题。模拟时应用程序的外观如何?故事板在这里告诉我们的不多。它们将是动态的。我添加了一个模拟器的截图。我没有看到模拟器的图片。能否在
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell
中设置断点以确保调用它?我想到了几件事。在interface builder中,单元格的标题标签是否约束在其容器的中心?如果是这样的话,接下来想到的是,可能你正在为细胞设置一个奇数大小。您正在使用func collectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize通过
UICollectionViewDelegateFlowLayout
?尝试让视图控制器符合
UICollectionViewDelegateFlowLayout
并添加
func collectionView(collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,SizeFormat indexPath:indexPath)->CGSize{返回CGSize(宽度:400,高度:400)}
并查看您现在是否可以看得更清楚一些。您需要确保将集合视图的
委托设置为self-Through。