Hi,
I’m attempting to do the bronze challenge using one datasource (the original one). I’ve added to item a value for section and set it in the init based on the value of the item:
if self.valueInDollars <= 50 {
self.section = 0
} else {
self.section = 1
}
I have it working perfectly if I stay in only one section (set the value to under or over 50 when generating) but once I work with two sections it breaks and I can’t figure out a workaround. Figured I’d ask here in case someone else has worked on it with the single shared datasource. My problem is when I add the new data. Specifically tableView.insertRows(at: [indexPath], with: .automatic)
. The first item works great and goes in it’s section (regardless of if the section is 0 or 1). Any other addition (if the section stays the same) works great. My issue is the first item for the other section and I get an error since the row for the item in the ItemStore doesn’t match up the row in the tableview.
For example the first three times I clicked add a value was added to section 0. The fourth time the value of the item was over $50 so it went in section 1 and I got the following error: Thread 1: "attempt to insert row 3 into section 1, but there are only 1 rows in section 1 after the update"
The method in question is:
@IBAction func addNewItem(_ sender: UIButton) {
// Create a new item and add it to the store
let newItem = itemStore.createItem()
// Figure out where that item is in the array
if let index = itemStore.allItems.firstIndex(of: newItem) {
var indexPath = IndexPath(row: index, section: newItem.section)
// Insert this new row into the table
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
Thank you for any help.