Assertion Error into Table

Hello I'm getting an assertion error that states


2018-02-07 08:45:43.245693-0500 Vinyl Vision[3631:122950] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:1770

2018-02-07 08:45:43.257778-0500 Vinyl Vision[3631:122950] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'


I use an alert message to import information into a table via textfield. Any suggestions? I also get a SIGBART on my AppDelegate file. Using XCode 9 and Swift 4.


import UIKit

import Firebase

import Foundation


class collectionViewController: UIViewController {


@IBOutlet weak var tableView: UITableView!


var desserts = [String]()


@IBAction func onAddTapped() {


let alert = UIAlertController(title: "Add Dessert", message: nil, preferredStyle: .alert)

alert.addTextField { (dessertTF) in

dessertTF.placeholder = "Enter Dessert"

}

let action = UIAlertAction(title: "Add", style: .default) { (_) in

guard let dessert = alert.textFields?.first?.text else { return }

print(dessert)

self.add(dessert)

}

alert.addAction(action)

present(alert, animated: true)

}


func add(_ dessert: String) {

let index = 0

desserts.insert(dessert, at: index)

let indexPath = IndexPath(row: index, section: 0)

tableView.insertRows(at: [indexPath], with: .left)

}

}

extension collectionViewController: UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {

return 1

}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return desserts.count

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = UITableViewCell()

let dessert = desserts[indexPath.row]

cell.textLabel?.text = dessert

return cell

}


func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

guard editingStyle == .delete else { return }

desserts.remove(at: indexPath.row)

tableView.deleteRows(at: [indexPath], with: .automatic)

}

}