0

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)
    }
}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29
Roggie
  • 1,157
  • 3
  • 16
  • 40

2 Answers2

1

You are probably asking for: pre select/highlight UICollectionViewCell on first load of view

Try this:

override func viewWillAppear(_ animated: Bool) {
let selectedIndexPath = IndexPath(item: 0, section: 0)
    collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .left)
}
Pedro Trujillo
  • 1,559
  • 18
  • 19
0

You are not the one to explicitly call the delegate methods of any API.

func collectionView(_:didSelectItemAt:) is the delegate method of UICollectionView. The api itself handles when to call this method. So you must not call this method from anywhere in your code.

Again your implementation of func collectionView(_:didSelectItemAt:) doesn't make any sense. This method will automatically be called when you tap on any of your cell. You don't have to deque your cell again here.

nayem
  • 7,285
  • 1
  • 33
  • 51
  • 1
    thanks Nayem for your input, as I said I'mm only a beginner and I trying to get my head around it. – Roggie Jul 05 '17 at 04:15