0

In my collection view I need to select the first cell as soon as the view appear.

I have implemented didSelectItemAtIndexPath and didDeselectItemAtIndexPath When the cell is selected it shows a green border when is not selected the cell has a white border.

What I need to have is to start my app with the first cell with a green border.

I have tried to use selectItemAtIndexPath however, the first cell is not showing the green border. What am I missing?

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        let firstIndexPath = NSIndexPath(forItem: 0, inSection: 0)

        frameCollectionView.selectItemAtIndexPath(firstIndexPath, animated: false, scrollPosition: .None)

    }

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let frameCell = collectionView.cellForItemAtIndexPath(indexPath) as! FrameViewCell

        frameCell.layer.borderWidth = 2
        frameCell.layer.borderColor = UIColor.greenColor().CGColor
    }

    func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {

        let frameCell = collectionView.cellForItemAtIndexPath(indexPath) as! FrameViewCell

        frameCell.layer.borderWidth = 1
        frameCell.layer.borderColor = UIColor.whiteColor().CGColor
    }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
SNos
  • 3,430
  • 5
  • 42
  • 92
  • At that point the cells are not loaded yet so you cannot select one. – Sulthan Apr 01 '16 at 22:10
  • I have also tried to add a delay of 5 seconds before the selection is called. But the cell is still not selected – SNos Apr 01 '16 at 22:18

2 Answers2

1

The delegate method are not called when you are selecting/deselecting programatically. They are called only if the user selects/deselects.

A better way is to change UI inside the cell class, see for example Trying to override "selected" in UICollectionViewCell Swift for custom selection state

Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • thank you! I have already looked at that answer however, it will select the cell only when scrolled and not when the view loads – SNos Apr 01 '16 at 22:37
  • @SNos When a cell loads, it is not selected. – Sulthan Apr 01 '16 at 22:39
  • Ok but there is a way to get only the first cell on view load with a green border? – SNos Apr 01 '16 at 22:42
  • `self.frameCollectionView.delegate?.collectionView!(self.frameCollectionView, didSelectItemAtIndexPath: NSIndexPath(forItem: 0, inSection: 0))` will select the cell however, when select another cell the first one is not deselected – SNos Apr 01 '16 at 23:01
1

Found the solution. In viewDidLoad():

utilityClass.delay(0.1) {
            let indexPathForFirstRow = NSIndexPath(forItem: 0, inSection: 0)
            self.frameCollectionView.selectItemAtIndexPath(indexPathForFirstRow, animated: false, scrollPosition: .None)
            self.collectionView(self.frameCollectionView, didSelectItemAtIndexPath: indexPathForFirstRow)
        }
SNos
  • 3,430
  • 5
  • 42
  • 92