-2

I am using an UISegmentedControl and have a switch with two cases set for each option. As of now the first case is selected when the view controller loads.

EDIT: How would I set the second case to be selected instead of the first only when the count is higher than the first.

Here is the code:

func collectionView(_ _collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    var count = 0

    switch(filmSeg.selectedSegmentIndex) {

    case 0:

        count = one.count 

        break

    case 1:

        count = two.count

        break

    default:
        break

    }

    return count

}
JSharpp
  • 459
  • 1
  • 9
  • 21
  • 1
    The first case applies because `filmSeg.selectedSegmentIndex` is 0. If you don’t like that, change the code or set `filmSeg.selectedSegmentIndex` to something else. – matt Nov 10 '18 at 20:32
  • 1
    Possible duplicate of [How can I set the default state of a UISegmentedControl?](https://stackoverflow.com/questions/1027424/how-can-i-set-the-default-state-of-a-uisegmentedcontrol) – Mo Abdul-Hameed Nov 10 '18 at 20:39
  • This question is for swift 4 and check the updated edit. – JSharpp Nov 10 '18 at 20:45

1 Answers1

1

As you can see here on the Apple Developer page, selectedSegmentIndex is declared as such:

var selectedSegmentIndex: Int { get set }

This means you can just put something like this in your viewDidLoad:

filmSeg.selectedSegmentIndex = 1
grooveplex
  • 2,492
  • 4
  • 28
  • 30