I'm a Swift beginner trying to implement a way to set a default cell in a CollectionView when the app first launches. I have overriden my 'isSelected' method to graphically show on the screen the selected cell eg; borderColor, etc...
My approach (rightly or not) so far is to call the 'didSelectItemAt' method inside my 'viewDidAppear' method.
I understand that the 'didSelectItemAt' method expects a CollectionView as its first argument, but not matter what I put as the first argument when I call the method inside my 'viewDidAppear' nothing seems to work.
any ideas?
extension DrinkMenuViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DrinkTypeImageCollectionViewCell", for: indexPath) as! DrinkTypeImageCollectionViewCell
cell.imgDrinkType.image = imageArray[indexPath.row]
cell.lbDrinkType.text = drinkTypeArray[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DrinkTypeImageCollectionViewCell", for: indexPath) as! DrinkTypeImageCollectionViewCell
cell.isSelected = true
print("selected cell: ", indexPath)
}
override func viewDidAppear(_ animated: Bool) {
let indexPathForFirstRow = IndexPath(row: 0, section: 0)
collectionView(<#T##collectionView: UICollectionView##UICollectionView#>, didSelectItemAt: indexPathForFirstRow)
print (indexPathForFirstRow)
}
}