title: Adding row to Static UITableView: Getting NSRangeException
body: <p>I've got a <code>static UITableView</code> with two <code>sections</code>. When the user taps the cell at index 2 in section 1, a <code>UITableViewCell</code> needs to be added below the tapped cell. Each subsequent cell tap should result in another cell being added below any already added cells.</p>
  
  <p>This is what I've tried so far, but I'm crashing with an <code>NSRangeException</code> stating that index 6 is beyond the available bounds so i'm clearly not updating the number of rows properly somehow..</p>
  
  <p>As per <a href="https://stackoverflow.com/questions/10043521/adding-unknown-number-of-rows-to-static-cells-uitableview">Adding unknown number of rows to &#39;Static Cells&#39; UITableView</a> I've overridden all <code>UITableView</code> methods that use an indexPath but their situation is slightly different to mine.</p>
  
  <pre><code>override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      if section == 1 {
          // subTaskRow is an array holding all the indexes of the added cells
          // so I know where to add the next one. I also know how many I've added
          return 6 + subTaskRow.count
      } else {
          return super.tableView(tableView, numberOfRowsInSection: section)
      }
  }
  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      // If the Sub Task cell is picked
      if (indexPath.section == 1 &amp;&amp; indexPath.row == 3) {
          // If this is the first subtask we want it to appear under Sub Tasks
          // We know the index of the cell below sub task so we can add it in directly
          if (subTaskRow.count == 0) {
              subTaskRow.addObject(5)
              tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 5, inSection: 1)], withRowAnimation: .Automatic)
              tableView.reloadData()
          // Other sub tasks should appear below previous sub tasks
          } else {
              // The last object in sub tasks will always be the
              // indexPath of the previous added cell
              let row = (subTaskRow.lastObject as! Int) + 1
              subTaskRow.addObject(row)
              tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 1)], withRowAnimation: .Automatic)
              tableView.reloadData()
          }
      }
  
      tableView.deselectRowAtIndexPath(indexPath, animated: true)
  }
  
  // Now we override all the other methods to ensure we don't get a crash
  override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -&gt; Bool {
      return false
  }
  
  override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -&gt; Bool {
      return false
  }
  
  override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCellEditingStyle {
      return .None
  }
  
  override func tableView(tableView: UITableView, indentationLevelForRowAtIndexPath indexPath: NSIndexPath) -&gt; Int {
      return super.tableView(tableView, indentationLevelForRowAtIndexPath: indexPath)
  }
  </code></pre>
  
  <p>My guess would be that adding in a cell in the middle of a section doesn't update the indexPaths of the already existing cells or something? I'm not sure how I'd do that though.</p>