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

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

我正在创建一个活动应用程序,其中 UITableView 中列出了数千名参与者。过程应该是checkIn(通过点击按钮)2个或更多参与者并拉动刷新。拉动刷新执行成功,但是当我尝试 pulling/scrolling 数据时,它崩溃并出现 Thread 1: Fatal error: Index out of range 。希望您能提供帮助,因为我尝试使用 SO 中的解决方案但仍然无法正常工作。谢谢你。

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

UIRefreshControl代码

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)
    }
}

这应该有帮助

filteredParticipants.count-10..<unsortedStrings.count

0...participants.count-10..<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)
        }
    }
}