-3

hi this is my code i keep getting the error when I delete a row. I know that my array is not being updated as i delete a row and hence the error. Orerlist.list is an array getting my data

How many i resolve this issue?

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) 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).'

class RootTableViewController: UITableViewController,MenuItemSelectionDelegate { var orderList = OrderList() var delegate:MenuItemSelectionDelegate! = nil

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // Return the number of sections.
    return 1
}


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

    // Return the number of rows in the section.
    return orderList.list.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)

    cell.textLabel?.text = orderList.list[indexPath.row].menuItem

    return cell
}


// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.

    return true
}



// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source

        self.tableView.beginUpdates()
        self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        self.tableView.endUpdates()




    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "reserve"{
        let vc = segue.destinationViewController as! BeautyViewController
        vc.delegate = self

    }
}



func didSelectMenuItem(controller: UITableViewController, order: OrderModel) {
    orderList.addList(order)
    controller.navigationController?.popViewControllerAnimated(true)
    tableView.reloadData()
}

}

dandan78
  • 13,328
  • 13
  • 64
  • 78
Ebin
  • 113
  • 1
  • 9

1 Answers1

0

You have to delete the corresponding element from the orderList.list as well.

Otherwise you tell the tableview you delete some data, it then re-queries the data and finds out that the data stayed the same - a contradiction to what you have just told it.

Something like

orderList.list.removeAtIndex(indexPath.row)
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • but the orderList is an array getting my data from another controller. – Ebin Dec 20 '15 at 16:51
  • @Ebin Why does that matter? Either you remove the element from the array or you remove the ability to remove elements from the tableview - on or the other. – luk2302 Dec 20 '15 at 16:52
  • haha thanks mate!. i didn't put in orderList.list. kept inputting it as just orderList. things should work out now :) – Ebin Dec 20 '15 at 16:54