title: Unable to insert new row to UITableView with dynamic number of Sections And Rows
body: <p>I have a tableView with dynamic number of sections and each section has dynamic number of rows. </p>
  
  <p>My datasource array looks like this </p>
  
  <pre><code>var detailsList = [[Any]]()
  </code></pre>
  
  <p>However I do know the type which would be added to the list and they would be added in a particular order. Lets consider these types would be A, B, C.</p>
  
  <p>Depending upon the availability of data from the API <code>detailsList</code> would be populated. Hence the array would look something like this:</p>
  
  <pre><code>[[A, A, A, A], [B, B, B], [C, C, C]]
  </code></pre>
  
  <p>In this example, the tableView has 3 sections and numberOfRows are dependent on the subarray count. </p>
  
  <p>Here is how the dataSource looks like</p>
  
  <pre><code>    extension ViewController: UITableViewDataSource {
              func numberOfSections(in tableView: UITableView) -&gt; Int {
                  return detailsList.count
              }
  
              func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
                  return detailsList[section].count
              }
  
              func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
                 if let _ = detailsList[indexPath.section] as? [A] {
                    return cellA
                 } else if let _ = detailsList[indexPath.section] as? [B] {
                   return cellB
                 } else if let _ = detailsList[indexPath.section] as? [C] {
                   return cellC
                  } else if let _ = detailsList[indexPath.section] as? [D] {
                   return cellD
                  }
   }
  </code></pre>
  
  <p>The problem which I am facing is when I want to insert a section.</p>
  
  <p>Lets say DataSource before adding a array looks like this </p>
  
  <pre><code>detailsList = [[A, A, A, A], [B, B, B], [C, C, C]]
  </code></pre>
  
  <p>After adding a new section the dataSource looks like this</p>
  
  <pre><code>detailsList = [[A, A, A, A], [B, B, B], [C, C, C], [D, D, D, D]]
  </code></pre>
  
  <p>I am unable to say </p>
  
  <pre><code>tableView.insertRows(at: [indexPath], with: .none)
  </code></pre>
  
  <p>App crashes with following exception</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception
    'NSInternalInconsistencyException', reason: 'Invalid update: invalid
    number of sections.  The number of sections contained in the table
    view after the update (4) must be equal to the number of sections
    contained in the table view before the update (3), plus or minus the
    number of sections inserted or deleted (0 inserted, 0 deleted).'</p>
  </blockquote>
  
  <p>But if i say</p>
  
  <p><code>tableView.reloadData()</code> it works as expected.</p>
  
  <p>is there another way to insert new section?</p>