I’m getting one of two errors when trying to implement Silver Challenge. When I include reloadDataI()
I’m getting the “Invalid batch updates” error below, and when excluding it I’m getting the “Invalid update: invalid number of sections” error. Please assist with some guidance. What I’m trying to do is have 1 section when there are no items, and 2 sections when there are items (where the 2 sections are “50 and under” and “over 50”).
Thread 1: "Invalid batch updates detected: the number of sections and/or rows returned by the data source before and/or after performing the batch updates are inconsistent with the updates.\nData source before updates = { 2 sections with row counts: [1, 0] }\nData source after updates = { 2 sections with row counts: [1, 0] }\nUpdates = [\n\tInsert row (0 - 0)\n]\nTable view: ..."
Thread 1: "Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). Table view: ...
Here’s the code for ItemsViewController:
class ItemsViewController: UITableViewController {
// add ItemStore property to have access to store that holds all items
var itemStore: ItemStore!
@IBAction func addNewItem(_ sender: UIButton){
let newItem = itemStore.createItem()
if newItem.valueInDollars > 50 {
if let index = itemStore.itemsOverFifty.firstIndex(of: newItem){
let indexPath = IndexPath(row: index, section: 1)
// tableView.reloadData()
tableView.insertRows(at: [indexPath], with: .automatic)
}
} else {
if let index = itemStore.itemsFiftyAndUnder.firstIndex(of: newItem){
let indexPath = IndexPath(row: index, section: 0)
// tableView.reloadData()
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
}
@IBAction func toggleEditingMode(_ sender: UIButton){
if isEditing{
sender.setTitle("Edit", for: .normal)
setEditing(false, animated: true)
} else {
sender.setTitle("Done", for: .normal)
setEditing(true, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if itemStore.allItems.count == 0 {
return "No Items!"
}
else if section == 0 {
return "50 AND UNDER"
} else {
return "OVER 50"
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return itemStore.allItems.count == 0 ? 1 : itemStore.numberOfSections
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if itemStore.allItems.count == 0 {
return 0
} else {
if section == 0 {
return itemStore.itemsFiftyAndUnder.count
} else {
return itemStore.itemsOverFifty.count
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
let item = indexPath.section == 0 ? itemStore.itemsFiftyAndUnder[indexPath.row] : itemStore.itemsOverFifty[indexPath.row]
cell.textLabel?.text = item.name
cell.detailTextLabel?.text = "$\(item.valueInDollars)"
return cell
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let item = indexPath.section == 0 ? itemStore.itemsFiftyAndUnder[indexPath.row] : itemStore.itemsOverFifty[indexPath.row]
itemStore.removeItem(item)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to toIndexPath: IndexPath) {
itemStore.moveItem(from: fromIndexPath.row, to: toIndexPath.row)
}
}