0

I make a table view on my application, I want to add option that could the user delete the row in tableview. I'm using this func h1 is my array string that I put it in tableview. my error is when I'm try to delete row in func editinstyle

  var h1:[String] = ["one","two","three"]


 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        h1.count
}
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        var cell = self.tableView.dequeueReusableCell(withIdentifier: "cell3", for: indexPath as IndexPath) as! TableView1

        cell.information.text = h1[indexPath.row]
}
  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            print("Deleted")

            h1.remove(at: indexPath.row) //Remove element from your array
            print(h1)
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

enter image description here when I try to click to Delete it show like thiserror 2020-02-11 20:43:35.803024+0200 TraniersApp[13314:401189] *** 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 (2) 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 (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Does someone know where is the problem?

3
  • Can you print h1 before calling remove(at:) to see whether remove(at:) actually does anything? Commented Feb 11, 2020 at 18:59
  • 2
    The code you have shown is correct, but I suspect that it isn't your actual code since the output in the screenshots is different. The exception indicates there is a problem with your numberOfRows Function. Also there appears to be another array value involved Commented Feb 11, 2020 at 19:01
  • @Paulw11 thank you, it works Commented Feb 11, 2020 at 19:09

2 Answers 2

1

To define "[indexPath]". It becomes [IndexPath(row: indexPath.row, section: 0)]

tableView.deleteRows(at: [IndexPath(row: indexPath.row, section: 0)], with: .automatic)

or

recall the particular section in the table view also

This will work outside of the function

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath)
Sign up to request clarification or add additional context in comments.

Comments

0

Suspect this is a race condition that can be resolved by performing the h1.remove() and tableView.deleteRows() within a transaction (i.e., between tableView.beginUpdates() and tableView.endUpdates()).

    tableView.beginUpdates()
    h1.remove(at: indexPath.row) //Remove element from your array
    tableView.deleteRows(at: [indexPath], with: .fade)
    tableView.endUpdates()

2 Comments

beginUpdates/endUpdates has no effect at all for a single insert/delete/move operation.
@vadian Hmm. I stand corrected. Something else that I've done in the intervening time appears to have corrected the real error that the transaction was masking. I was able to remove the transaction without error. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.