0
votes

When I delete the last row from a tableview with:

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

and change my table's data source to an another array:

if orders.isEmpty {
    return ABCDArray.count
} else {
    return orders.count
}

It gives me this error:

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 (20) must be equal to the number of rows contained in that section before the update (1), 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).'

How can I solve that? What is the problem with changing data source?

2
tableView.deleteRows(at: [indexPath], with: .middle) Que!? - El Tomato

2 Answers

0
votes

You need to remove the object from your data array before you call tableView.deleteRows(at: [indexPath], with: .middle). So, your code should look like this:

// Editing of rows is enabled
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        //when delete is tapped
        orders.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .middle)
    }
}
0
votes

Was an interesting question so I decided to make it a try.

I made a quick implementation with that, I am sure it's not the prettiest way to do it but if you can be inspired by that maybe:

class ViewController : UITableViewController {

    var orders = [1,2,3,4]
    var ABCDArray = ["A","B","C","D"]

    var currentCellNumber = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        currentCellNumber = orders.count

        tableView?.delegate = self
        tableView?.dataSource = self
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        if orders.isEmpty {
            cell.textLabel?.text = ABCDArray[indexPath.row]
        } else {
            cell.textLabel?.text = String(orders[indexPath.row])
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return currentCellNumber
    }


    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if !orders.isEmpty {
            orders.remove(at: indexPath.row)
            currentCellNumber -= 1
            tableView.deleteRows(at: [indexPath], with: .middle)

            if orders.isEmpty {
                changeDataSource(newDataSourceArray: ABCDArray)
            }

        }
    }

    func changeDataSource(newDataSourceArray array: Array<Any>) {
        var newCellIndexPaths = [IndexPath]()
        for row in 0...array.count-1 {
            print(row)
            currentCellNumber = row+1
            print(currentCellNumber)
            let insertionIndexPath = IndexPath(row: row, section: 0)
            print(insertionIndexPath)
            newCellIndexPaths.append(insertionIndexPath)

        }
        tableView.insertRows(at: newCellIndexPaths, with: .automatic)
    }

}

If you have some question donut hesitate> Hope it helps