UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Project 5 - tableView inserting causing crash "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update"

Forums > 100 Days of Swift

So I'm following through with project 5 and have followed closely until the end of the first section of part 2. When I compile and run, I run into the error "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update". This error occurs only after I click Submit to enter my answer so that it can be inserted at row: 0, section: 0. I have double checked my code and can not find a difference between mine and the one Paul provides.

Here is the code for my submit function

func submit(_ answer: String) {
        let lowerAnswer = answer.lowercased()

        if isPossible(word: lowerAnswer) {
            if isOriginal(word: lowerAnswer) {
                if isReal(word: lowerAnswer) {
                    usedWords.insert(answer, at: 0)

                    let indexPath = IndexPath(row: 0, section: 0)

                    tableView.insertRows(at: [indexPath], with: .automatic) //the exact line that seems to be causing my crash

                }
            }
        }
    }

Here is the code for my promptForAnswer function as well, in case that is of use:

@objc func promptForAnswer() {
        let ac = UIAlertController(title: "Enter answer", message: nil, preferredStyle: .alert)
        ac.addTextField()

        let submitAction = UIAlertAction(title: "Submit", style: .default) { 
            [weak self, weak ac] action in 

            guard let answer = ac?.textFields?[0].text else { return }
            self?.submit(answer)
        }

        ac.addAction(submitAction)
        present(ac, animated: true)
    }

3      

Okay that is confusing, but can you show your tableView load data?

3      

Yeah super confusing, to me esspecially, I'm assuming this is what you need?

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Word", for: indexPath)
        cell.textLabel?.text = usedWords[indexPath.row]
        return cell
    }

3      

What are you returning in numberOfRowsInSection?

6      

To those who also may run into this problem, When adding

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return usedWords.count
    }

I accidently pressed tab to autofill when typing in "numberOf" and it autofilled to

   override func numberOfSections(in tableView: UITableView) -> Int {
        return usedWords.count
    }

and I hadn't noticed. This is what was causing the crash and removing this function fixed my code and is now running as it is supposed to. Thanks @roosterboy for suggesting I take a look at the numberOfRowsInSection portion of my code!

7      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.