Bronze Challenge Using 1 DataSource

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. :slight_smile:

The problem is in this line:

    if let index = itemStore.allItems.firstIndex(of: newItem) {

Instead of getting the position of the new item relative to all other items, you need to get it relative to only the other items in that section. I used a filter on itemStore to return a container containing only the items in that section & then got the firstIndex from that.

Thanks I started using a filter in itemStore but wasn’t able to make it visible outside the method so I started trying to do random other things to get it to work. Maybe you can help me with the filter. I used a method (in itemStore) that is
func allItemsInSection( _ item: Item) -> [Item] {
but when I go to use it in itemViewController:
let index = itemStore.allItemsInSection(newItem).firstIndex(of: newItem)
I get an error : Value of type ‘ItemStore’ has no member ‘allItemsInSection’

Thanks

I don’t know. Seems like that should work, it’s very close to how I did it. Does cleaning the build folder (on the Product menu) and rebuilding help? When I start getting errors that appear to make no sense (like claiming that a function doesn’t exist), sometimes doing that will help.

Didn’t think to try cleaning the build folder so thank you. That said it still doesn’t work. Thanks for your help though

I was working in the wrong file (since I had copied the project) so was working in the original and expecting the new one to run. Stupid mistake. Onto the next problem but thank you.