title: SwiftUI: how to update the range of a ForEach loop based on the value of a Picker
body: <p>So I'm trying to have a ForEach loop update the amount of times a View is looped based on what Month (value in a picker) is selected. In my case, they will be looping based on the number of days in the month of the selected month for the given year. I already have a function that gives me the number of days in each month, however, when I plug that into the ForEach Loop, it only gets run based on the first month selected and stays iterating the number of days of that month for the rest. Here is my code for the ForEach Loop:</p>
  <pre><code>ForEach(0..&lt;getRange(year: yearIndex, month: monthIndex + indexCheck)) { i in
       NavigationLink(destination: ContentView(day: yearData[yearIndex].months[monthIndex].dayInfo[i])) {
            DayRow(day: yearData[yearIndex].months[monthIndex].dayInfo[i])
       }
  }
  </code></pre>
  <p>and here is the getRange() Function:</p>
  <pre><code>func getRange(year: Int, month: Int) -&gt; Int {
          return Calendar.current.range(of: .day, in: .month, for: Calendar.current.date(from: DateComponents(year: year + 2020, month: month + 1))!)!.count
  }
  </code></pre>
  <p>The yearIndex variable is linked to the picker value of three years, (2020, 2021, 2022). Here is the code for it:</p>
  <pre><code>Picker(&quot;Years&quot;, selection: $yearIndex) {
       ForEach(0 ..&lt; year.count) { i in
            Text(String(self.year[i])).tag(i)
       }
  }
  .pickerStyle(SegmentedPickerStyle())
  </code></pre>
  <p>The monthIndex variable is linked to the picker with the months in the year (Jan-Dec). Here is the code for it:</p>
  <pre><code>Picker(&quot;Month&quot;, selection: $monthIndex) {
       ForEach(0 ..&lt; monthArray.count) { i in
            Text(self.monthArray[i]).tag(i)
       }
  }
  .padding(.bottom, 2)
  </code></pre>
  <p>I'm not sure what I'm doing wrong, and I'm not sure how to do this, so any help would be greatly appreciated! I'm still quite new to Swift/SwiftUI, so any advice to better code this would also be appreciated!</p>
  <p><strong>EDIT: Here is a minimal reproducible example as requested:</strong></p>
  <pre><code>struct ContentView: View {
      
      @State var year = [2020, 2021, 2022]
      //monthSymbols gets an array of all the months
      @State var monthArray = DateFormatter().monthSymbols!
      @State var yearIndex = 0
      @State var monthIndex = 0
      @State var indexCheck = 0
      @State var indexTest = 0
      
      var body: some View {
          NavigationView {
              List {
                  Section {
                      VStack {
                          Picker(&quot;Years&quot;, selection: $yearIndex) {
                              ForEach(0 ..&lt; year.count) { i in
                                  Text(String(self.year[i])).tag(i)
                              }
                          }
                          .pickerStyle(SegmentedPickerStyle())
                          
                          Divider()
                          
                              Picker(&quot;Month&quot;, selection: $monthIndex) {
                                  ForEach(0 ..&lt; monthArray.count) { i in
                                      Text(self.monthArray[i]).tag(i)
                                  }
                              }
                              .padding(.bottom, 2)
                          }
                      }
                      Section(header: Text(&quot;What I love about you&quot;)) {
                          ForEach(0..&lt;getRange(year: yearIndex, month: monthIndex + indexCheck)) { i in
                              NavigationLink(destination: DetailsView()) {
                                  Text(&quot;Row \(i)&quot;)
                              }
                          }
                      }
              }
              .listStyle(InsetGroupedListStyle())
              .navigationBarTitle(Text(&quot;\(monthArray[monthIndex + indexCheck]) \(String(year[yearIndex]))&quot;))
          }
          
      }
      func getRange(year: Int, month: Int) -&gt; Int {
          return Calendar.current.range(of: .day, in: .month, for: Calendar.current.date(from: DateComponents(year: year + 2020, month: month + 1))!)!.count
      }
  }
  
  struct YearView_Previews: PreviewProvider {
      static var previews: some View {
          ContentView()
      }
  }
  </code></pre>