title: Can't insert new row in tableView
body: <p>I understand that the problem has been solved many times, but none of the ways helped me. </p>
  
  <p>I want to insert new row in tableView. I have three sections and in second and third sectons I have UIButton which can infert new row to the desired section, but I receive error in console. what could be the problem?</p>
  
  <p>My error when I insert new row - <code>attempt to insert row 2 into section 1, but there are only 2 rows in section 1 after the update</code></p>
  
  <p>And my code:</p>
  
  <pre><code>class SettingsScheduleAndPricesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  
      @IBOutlet weak var tableView: UITableView!
      @IBOutlet weak var saveBarButtonItem: UIBarButtonItem!
      @IBOutlet weak var backBarButtonItem: UIBarButtonItem!
  
      let sectionTitle = ["Day of week", "Second section", "Third section"]
      var secondRowText = ["First row", "Second row"]
      var thirdRowText = ["Row one", "Row two", "Row three"]
  
      override func viewDidLoad() {
          super.viewDidLoad()    
      }
      // MARK: - TableView
      func numberOfSections(in tableView: UITableView) -&gt; Int {
          return sectionTitle.count
      }
  
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          switch section {
          case 0: return 1
          case 1: return 2
          default: return 3
          }
      }
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          if indexPath.section == 0 {
              let collectionCell = tableView.dequeueReusableCell(withIdentifier: "collectionCell", for: indexPath) as! SettingsCollectionViewCell
              return collectionCell
          } else if indexPath.section == 1 {
              let hourlyRateCell = tableView.dequeueReusableCell(withIdentifier: "hourlyRateCell", for: indexPath) as! SettingsHourlyRateCell
              hourlyRateCell.textLabel?.text = secondRowText[indexPath.row]
              return hourlyRateCell
          } else {
              let datePriceCell = tableView.dequeueReusableCell(withIdentifier: "datePriceCell", for: indexPath) as! SettingsDatePriceCell
              datePriceCell.textLabel?.text = thirdRowText[indexPath.row]
              return datePriceCell
          }
      }
  
      func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
          return self.sectionTitle[section]
      }
  
      func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
          let frame: CGRect = tableView.frame
          if section == 0 {
              let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
              headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
              return headerView
          } else if section == 1 {
              let addButton: UIButton = UIButton(frame: CGRect(x: frame.size.width - 50, y: 0, width: 50, height: 30))
              addButton.backgroundColor = UIColor.clear
              addButton.setTitleColor(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1), for: .normal)
              addButton.setTitle("Add", for: .normal)
              addButton.addTarget(self, action: #selector(SettingsScheduleAndPricesViewController.addHourlyRate(sender:)), for: .touchUpInside)
              let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
              headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
              headerView.addSubview(addButton)
              return headerView
          } else {
              let addButton: UIButton = UIButton(frame: CGRect(x: frame.size.width - 50, y: 0, width: 50, height: 30))
              addButton.backgroundColor = UIColor.clear
              addButton.setTitleColor(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1), for: .normal)
              addButton.setTitle("Add", for: .normal)
              addButton.addTarget(self, action: #selector(SettingsScheduleAndPricesViewController.addDatePrice(sender:)), for: .touchUpInside)
              let headerView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
              headerView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
              headerView.addSubview(addButton)
              return headerView
          }
      }
  
      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {
          return 30
      }
  
      @objc func addHourlyRate(sender: UIButton) {
          let newRow: String = "Third row"
          secondRowText.append(newRow)
          let indexPath = IndexPath(row: secondRowText.count - 1, section: 1)
          tableView.beginUpdates()
          tableView.insertRows(at: [indexPath], with: .automatic)
          tableView.endUpdates()
      }
  
      @objc func addDatePrice(sender: UIButton) {
          let newRow: String = "Row four"
          thirdRowText.append(newRow)
          let indexPath = IndexPath(row: thirdRowText.count - 1, section: 2)
          tableView.beginUpdates()
          tableView.insertRows(at: [indexPath], with: .automatic)
          tableView.endUpdates()
      }
  }
  </code></pre>
  
  <p><code>delegate</code> and <code>dataSource</code> I added in <code>storyboard</code></p>
  
  <p>Please do not scold for repeating the question, nothing helps me of all the answers that I found</p>