ken
ken

Reputation: 668

Failed to insert data into UITableView in swift

I would like to insert a new data array into the UITableView without using reloadData() method.

I created simple code to do that. I referred to this discussion. However, error is occurred. I investigated why error is occurred, but I could not find it out.

My source code is here.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var objects = [
        "Apple",
        "Orange"
    ]

    let insertObjects = [
        "Banana",
        "Grape"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func insert(_ sender: Any) {
        self.objects = self.insertObjects + self.objects
        tableView.beginUpdates()
        let indexPath = IndexPath(row: 0, section: 0)
        tableView.insertRows(at: [indexPath], with: .automatic)
        tableView.endUpdates()
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.objects.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

        cell.label.text = self.objects[indexPath.row]
        return cell
    }
}

And, error message is here.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (4) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:

Could give me your advice ?

Upvotes: 1

Views: 478

Answers (2)

pacification
pacification

Reputation: 6018

You should rewrite this function like:

@IBAction func insert(_ sender: Any) {
    objects = insertObjects + objects
    let indexPaths = (0..<insertObjects.count).map({ IndexPath(row: $0, section: 0) })
    tableView.insertRows(at: indexPaths, with: .automatic)
}

Because the insertObjects has 2 elements, you need to reload two IndexPaths or just call tableView.reloadData() instead of begin/endUpdates code.

Upvotes: 1

vadian
vadian

Reputation: 285260

When inserting new objects after the first reload you have to provide an IndexPath for each inserted object.

As the items are inserted at the beginning of the table this solution counts the number of new items and maps the indexes to IndexPaths

@IBAction func insert(_ sender: Any) {
    let numberOfItemsToInsert = self.insertObjects.count
    let indexPaths = (0..<numberOfItemsToInsert).map{IndexPath(row: $0, section: 0)}
    self.objects = self.insertObjects + self.objects
    tableView.insertRows(at: indexPaths, with: .automatic)
}

As always, begin-/endUpdates() is not needed for a single insert operation.

Upvotes: 1

Related Questions