Beginning Table Views · Refactoring the Model | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5995-beginning-table-views/lessons/51

Hi, Brian! I’ve done everything as like as you showed in the videos. However, I always stuck in a problem: when I press the “Done” button while adding a new item, there is an error that says: attempt to insert row 5 into section 0, but there are only 0 rows in section 0 after the update. I downloaded your project in the link below and ran it. There is a similar problem. Please solve it as fast as you can. Thanks.

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Without seeing your code, it’s really hard diagnose. My suggestion is to compare your completed project with the final project included with the downloadable files.

Thanks!

Hello @bdmoakley. I have the same problem as @anon81482478.
Now that we have sections for each letter in the alphabet, the problem is that when we add any new item, we do not reference a correct row and section for it to go into. We simply tell it to go into section 0 (the default we had been working with up until this point), and the row index is 5 because we had those 5 ChecklistItems hardcoded in our todos array. And so, as @anon81482478 pointed out, we are getting the error “attempt to insert row 5 into section 0, but there are only 0 rows in section 0 after the update

Editing an item also presents an issue as it does not get sorted alphabetically after the edit.

    func itemDetailViewController(_ controller: ItemDetailViewController, didFinishAdding item: ChecklistItem) {
    navigationController?.popViewController(animated: true)
    let rowIndex = todoList.todos.count - 1
    let indexPath = IndexPath(row: rowIndex, section: 0)
    let indexPaths = [indexPath]
    tableView.insertRows(at: indexPaths, with: .automatic)
  }
  
  func itemDetailViewController(_ controller: ItemDetailViewController, didFinishEditing item: ChecklistItem) {
    if let index = todoList.todos.index(of: item) {
      let indexPath = IndexPath(row: index, section: 0)
      if let cell = tableView.cellForRow(at: indexPath) {
        configureText(for: cell, with: item)
      }
    }
    navigationController?.popViewController(animated: true)
  }

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

This looks like an oversight when I made the course. I was most likely unaware this created an issue when adding new items. That said, it gets resolved later in the course. So don’t worry about it now, just keep trucking along. We’ll make sure to address this when we update this is in the coming year.

On the new ‘remove’ method, how come you passed in a checklistitem. I didn’t see you use it anywhere in the method.

Yes, that’s an oversight. The parameter should be removed and the method renamed.

1 Like