cellHeight: поток 1: фатальная ошибка: индекс выходит за пределы диапазона при извлечении данных в UITableView - PullRequest
0 голосов
/ 04 октября 2018

Я создаю приложение для событий, в котором тысячи участников перечислены внутри UITableView.Процесс должен быть на checkIn (нажав кнопку) 2 или более участников и потяните, чтобы обновить.Pull для обновления выполняется успешно, но когда я попытался потянуть / прокрутить данные, он вылетает и появляется Thread 1: Fatal error: Index out of range.Надеюсь, вы могли бы помочь, потому что я пытался использовать решения в SO, но все еще не работал.Спасибо.error here

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

1 Ответ

0 голосов
/ 04 октября 2018

Это должно помочь

filteredParticipants.count-1 ИЛИ 0..<unsortedStrings.count

0...participants.count-1 ИЛИ 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)
        }
    }
}
...