skip to Main Content

so i have collectionview where i want the first cell of the collectionview cell is selected when the view is load, i try to use viewDidAppear method and…..well not work very good.

cause for the very first time i try to load the view the first collectionview cell is not selected, but when i back to previous controller and then tap back to the collectionview controller, the first cell of the collectionview is selected.

i mean i dont know what i have to go back and forth so the first cell can automatically be selected.

heres my code :

    @IBOutlet weak var katalogColView: UICollectionView!

     let indexPos = NSIndexPath(row: 0, section: 0) as IndexPath

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        self.katalogColView.selectItem(at: indexPos, animated: false, scrollPosition: [])
        self.collectionView(katalogColView, didSelectItemAt: indexPos)
    }

     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == katalogColView {
            
            guard let cell = collectionView.cellForItem(at: indexPath) as? KatalogCell else {return}
 
            cell.indicator.backgroundColor = #colorLiteral(red: 0.9916914105, green: 0.2737390399, blue: 0.284696877, alpha: 1)
            cell.katalogText.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
       }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        if collectionView == katalogColView {
            guard let cell = collectionView.cellForItem(at: indexPath) as? KatalogCell else {return}
            
            cell.indicator.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
            cell.katalogText.textColor = #colorLiteral(red: 0.3950070143, green: 0.4162634611, blue: 0.4362195134, alpha: 1)

        
        }
    }

so the code is when the cell is selected, there’s a uiview indicator that colored red if selected, and white if not selected

is there something wrong with my code? i mean of course there’s something wrong but can anyone tell me why its not working?

i mean like i said it only work if i go back to previous controller, and then i go back to that collectionView controller where it automatically selected first cell, but not selected automatically at the first time i load the controller.

Thanks

2

Answers


  1. Add these lines in cell class and try:

        override var isSelected: Bool {
        didSet {
            if isSelected {
                indicator.backgroundColor = #colorLiteral(red: 0.9916914105, green: 0.2737390399, blue: 0.284696877, alpha: 1)
                katalogText.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
            }else {
                indicator.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                katalogText.textColor = #colorLiteral(red: 0.3950070143, green: 0.4162634611, blue: 0.4362195134, alpha: 1)
            }
        }
    }
    
    Login or Signup to reply.
  2. You have to select it from visibleCells.

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(true)
        
        let selectedIndex = collectionView.indexPathsForVisibleItems![0]
        collectionView.selectItem(at: selectedIndex, animated: true, scrollPosition: .none)
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search