问题描述
我试图找出如何在collectionView启动时以编程方式选择一个单元格.与之类似,collectionView的默认加载应选择第一个单元格.
I'm trying to figure out how to programmatically select a cell the moment the collectionView starts up. As in, the default load of the collectionView should have the first cell selected.
我在选择功能中使用了shouldSelectItemAtIndexPath,因此我想针对特定的单元格索引调用它.
I'm using shouldSelectItemAtIndexPath for my selection function, so I want to call it for a specific cell index.
到目前为止,我已经尝试过:
So far I have tried:
collectionView(self.collectionView!, shouldSelectItemAtIndexPath: self.collectionView!.indexPathForCell(CVcellArray.first!)!)
在我的自定义CollectionView脚本的viewDidLoad函数中.
In my viewDidLoad function for my custom CollectionView script.
在加载collectionView时崩溃.我在做错什么吗?
It crashed when the collectionView loaded. Am I doing something wrong?
谢谢.
推荐答案
在viewDidAppear
中,只需调用selectItemAtIndexPath
.
这篇关于Swift-首次加载collectionView时如何自动选择单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!