Home > front end >  Fatal error: Index out of range (on cellForRowAt tableView func)
Fatal error: Index out of range (on cellForRowAt tableView func)

Time:08-16

I'm trying to assign values to some text and getting a fatal error:

Thread 1: Fatal error: Index out of range

Here is my func:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.row < 5) {
            let cell:TaskCell = tableView.dequeueReusableCell(withIdentifier: "TaskCell", for: indexPath) as! TaskCell
            let taskIndex = indexPath.row
            print(taskIndex)
    
            if let task = card?.tasks[taskIndex] {
                cell.taskText.text = task.text
                cell.taskBtn.isSelected = task.complete
            }
            
            cell.selectionStyle = .none
            
            return cell
        } else {
            let cell:GratefulCell = tableView.dequeueReusableCell(withIdentifier: "GratefulCell", for: indexPath) as! GratefulCell
            let gratefulIndex = indexPath.row
            print(gratefulIndex)
            cell.gratefulNum.text = String(gratefulIndex - 4)
            
            if let grateful = card?.gratefuls[gratefulIndex] {   <-------ERROR HERE.
                cell.gratefulText.text = grateful.text
            }
            
            cell.selectionStyle = .none

            return cell
        }
    }

I'm filling out text in every box available on my screen. (9 total, 5 for tasks, 4 for gratefuls)

When I print(taskIndex) and print(gratefulIndex) I get:

task 1
task 2
task 3
task 4
grateful 5
grateful 6
grateful 7
grateful 8

When I save my "card" (which is a single screen holding tasks and gratefuls) then go to reopen the card I get

task 0
task 1
task 2
task 3
task 4
grateful 5

Then it dies... any help would be greatly appreciated, I'm sure its starring right at me but I can't figure it out. If you need any additional information please let me know. Thank you!

CodePudding user response:

It looks to me like you should use a sectioned table view with 2 sections, one for tasks, and one for "gratefuls". The row will start at 0 for each section, so you can use the row number to index into both your tasks and your gratefuls.

The way you've written your table view code, your first gratefuls will try to index into your gratefuls array at index card?.tasks.count.

You need to start at index 0 of your gratefuls. If you're going to do it all with one section, change this bit:

        if indexPath.row <= (card?.tasks.count ?? 0) {
           // Tasks code.
        } else {
            let cell:GratefulCell = tableView.dequeueReusableCell(withIdentifier: "GratefulCell", for: indexPath) as! GratefulCell
            // Below is the change to make your gratefulIndex start at 0, the first item in your array `gratefuls`.
            let gratefulIndex = indexPath.row - (card?.tasks.count ?? 0)
            print(gratefulIndex)
            cell.gratefulNum.text = String(gratefulIndex - 4)
            
            if let grateful = card?.gratefuls[gratefulIndex] {  
                cell.gratefulText.text = grateful.text
            }
            
            cell.selectionStyle = .none

            return cell
        }
  • Related