Search code examples
iosswiftuitableviewuirefreshcontrol

cellHeight: Thread 1: Fatal error: Index out of range when pulling up data in UITableView


I am creating an Event App wherein thousands of participants are listed inside UITableView. The process should be to checkIn(by tapping a button) 2 or more participants and pull to refresh. Pull to refresh executes successfully, but when I tried pulling/scrolling up the data, it crashes and appears Thread 1: Fatal error: Index out of range . Hope you could help because I tried to use the solutions in SO but still not working. Thank you. error here

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return cellHeights[indexPath.row]
}

UIRefreshControl code

var refresher: UIRefreshControl!

 override func viewDidLoad() {
    super.viewDidLoad()

    createCellHeightsArray()

    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresher.addTarget(self, action: #selector(ParticipantsViewController.refresh), for: UIControlEvents.valueChanged)
    ParticipantTableView.addSubview(refresher)

    countNotif()
    getData()

}

  @objc func refresh() {
    countNotif()
    getParticipants()
    refresher.endRefreshing()
    ParticipantTableView.reloadData()
   }
  func getData() {
    getParticipants()

    if let posts = participants {
        self.participants = posts
    } else {
    self.participants.removeAll()
}
    self.refresh()
}

cellHeightArray

  func createCellHeightsArray() {

    cellHeights.removeAll()

    if searchController.isActive && searchController.searchBar.text != "" {
        for _ in 0...filteredParticipants.count {
            cellHeights.append(kCloseCellHeight)
        }
    }else {
        for _ in 0...participants.count {
            cellHeights.append(kCloseCellHeight)
        }
    }
}

 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    guard case let cell as ParticipantCell = cell else {
        return
    }

    cell.backgroundColor = UIColor.clear

    if searchController.isActive && searchController.searchBar.text != "" {
        cell.participant = filteredParticipants[indexPath.row]
    }else {
        cell.participant = participants[indexPath.row]
    }

    if cellHeights[(indexPath as NSIndexPath).row] == kCloseCellHeight {
        cell.unfold(false, animated: false, completion: nil)
    } else {
        cell.unfold(true, animated: false, completion: nil)
    }
}

Solution

  • This should help

    filteredParticipants.count-1 OR 0..<unsortedStrings.count

    0...participants.count-1 OR 0..<participants.count

    func createCellHeightsArray() {
    
        cellHeights.removeAll()
    
        if searchController.isActive && searchController.searchBar.text != "" {
            for _ in 0..<filteredParticipants.count {
                cellHeights.append(kCloseCellHeight)
            }
        }else {
            for _ in 0..<participants.count {
                cellHeights.append(kCloseCellHeight)
            }
        }
    }