user3488148
user3488148

Reputation: 59

Swift - how automatically select cell when collectionView first loads up?

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.

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!)!)

In my viewDidLoad function for my custom CollectionView script.

It crashed when the collectionView loaded. Am I doing something wrong?

Thank you.

Upvotes: 2

Views: 5138

Answers (1)

Patrick Tescher
Patrick Tescher

Reputation: 3447

In viewDidAppear just call selectItemAtIndexPath.

Upvotes: 7

Related Questions