简体   繁体   中英

Set default cell in a UICollectionView (SWIFT)?

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM