Search code examples
arraysswiftuitableviewuiviewcontrollercustom-cell

Deleting cell giving error and crashing app


I need some help figuring out why I am getting this error. I currently have a view controller (let's call it ADDPersonVC) appending a struct. The struct looks like the following:

import UIKit

struct Person {
    var Name: String?
    var PostNumber: String?
    var StoryNumber: String?
    var Compensation: String?
    var ProfileImageUrl: String?
}

I have a button in ADDPersonVC that appends labels in a second view controller's (let's call it considerationsTestVC) tableViewCell. The number of rows is determined by:

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

The addPersonButton code that appends the struc looks like:

@IBAction func addButtonTapped(_ sender: Any) {
    
    ConsiderationsTestViewController.people.append(Person(Name: "\(nameTextField.text!)", PostNumber: "\(gridTextField.text!)", StoryNumber: "\(storyTextField.text!)", Compensation: "\(compensationTextField.text!)", ProfileImageUrl: "\(userProfileImageUrlLabel.text!)"))
    
    self.navigationController?.popViewController(animated: true)
    
    DispatchQueue.main.async {
       NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
            }
}

In ConsiderationsTestViewController I have a static var called people that looks like:

static var people: [Person] = []

I then am able to append the labels in the ConsiderationsTestViewContoller with the following code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: AddPersonCell, for: indexPath) as! ConsiderationsCell
    
    let numberOfPeopleCells = ConsiderationsTestViewController.people[indexPath.row]
    
    
    cell.nameLabelC.text = numberOfPeopleCells.Name
    cell.feedLabelC.text = numberOfPeopleCells.PostNumber
    cell.storyLabelC.text = numberOfPeopleCells.StoryNumber
    cell.compensationLabelC.text = numberOfPeopleCells.Compensation
    cell.userImage.loadImageUsingCacheWithUrlString(urlString: numberOfPeopleCells.ProfileImageUrl!)
    
    cell.userImage.layer.cornerRadius = 25
    
    cell.nameLabelC.numberOfLines = 0
    
    return cell
}

When I go to delete the cell I am using the code:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == .delete) {
        
        ConsiderationsTestViewController.people.remove(at: indexPath.row)
        // handle delete (by removing the data from your array and updating the tableview)
    }
}

When I slide on the cell and click delete I am getting the error:

 "Thread 1: Fatal error: Index out of range"

at the line:

ConsiderationsTestViewController.people.remove(at: indexPath.row)

I've been researching this all week and would love some help with trying to solve this problem. I am happy that I am able to get the struct to act as an array and display the proper information correctly. I am also having a problem uploading it to Firebase - realtimeDatabase but that is a question for after I figure this out.

I appreciate all the help.

EDIT:

When I run a breakpoint on the delete function and get values it returns:

3 elements
 ▿ 0 : Person
 ▿ Name : Optional<String>
  - some : "Person's Name1"
 ▿ PostNumber : Optional<String>
  - some : "1"
 ▿ StoryNumber : Optional<String>
  - some : "1"
 ▿ Compensation : Optional<String>
  - some : "1"
 ▿ ProfileImageUrl : Optional<String>
  - some : "PROFILEIMAGE URL HERE"
 ▿ userID : Optional<String>
  - some : "B2Z4DlZ8RucvEQhz2NSUkquqc5P2"

 ▿ 1 : Person
 ▿ Name : Optional<String>
  - some : "Person's Name2"
 ▿ PostNumber : Optional<String>
  - some : "1"
 ▿ StoryNumber : Optional<String>
  - some : "11"
 ▿ Compensation : Optional<String>
  - some : "1"
 ▿ ProfileImageUrl : Optional<String>
  - some : "PROFILEIMAGE URL HERE"
 ▿ userID : Optional<String>
  - some : "Lbn9HL1WIpZTMFzpGWAXy7Ra0EA2"

 ▿ 2 : Person
 ▿ Name : Optional<String>
  - some : "Person's Name3"
 ▿ PostNumber : Optional<String>
  - some : "1"
 ▿ StoryNumber : Optional<String>
  - some : "1"
 ▿ Compensation : Optional<String>
  - some : "1"
 ▿ ProfileImageUrl : Optional<String>
  - some : "PROFILEIMAGE URL HERE"
 ▿ userID : Optional<String>
  - some : "NE6WVUfF6WQjJT9eeVSJgfvrrZW2"

I also tried this:

 po ConsiderationsTestViewController.people[indexPath.row]

I received the following:

 Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444
  2020-08-06 11:14:34.945249-0700 BWM[10260:384634] Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.13/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444

Solution

  • I needed to add the following code in my delete function"

    tableView.deleteRows(at: [indexPath], with: .fade)