Reputation: 881
I've got a An array which is a an array of Sections in the Table View. I have got another array that contain arrays in it. I displayed it on the table view correctly, but each time I want to delete it, I get an error. I want to delete the exactly item in the array which is inside the array. Here is what I got so far:
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var eachSection = ["a","ss","dd","heyyyyyy","3"]
var eachStep = [["z","zz"],["x","xx"],["c","cc","ccc"],["r","rr","rrr","rrrr"],["o"]]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return eachSection.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sectionString = Array(eachStep)[section]
return sectionString.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// Configure the cell...
let sectionString = Array(eachStep)[indexPath.section]
cell.textLabel?.text = sectionString[indexPath.row]
print(sectionString)
return cell
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let sectionString = eachSection[section]
return sectionString
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if (editingStyle == UITableViewCellEditingStyle.Delete) {
var sectionString = Array(eachStep)[indexPath.section]
sectionString.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
}
This is the error I got:
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)
Upvotes: 1
Views: 182
Reputation: 15512
This error contains answer for youre question.
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).
Look carefully what are you doing.
You delete items from one array that you are not using as data source.
var sectionString = Array(eachStep)[indexPath.section]
sectionString.removeAtIndex(indexPath.row)
Try this:
eachStep[indexPath.section].removeAtIndex(indexPath.row)
Upvotes: 2