After appending a new cellType my locationTableViewCell is giving an error at let place = places[indexPath.row] while entering text inside the text field search bar.
private func locationTableViewCell(indexPath: IndexPath) -> PostSelectLocationTableViewCell {
let locationCell = tableView.dequeueReusableCell(forIndexPath: indexPath) as PostSelectLocationTableViewCell
let place = places[indexPath.row]
locationCell.configure(model: place)
return locationCell
}
locationInteractor.didPlacesLoaded = { [weak self] places in
DispatchQueue.main.async {
self?.places = places
if self?.places.isEmpty == true {
self?.cellTypes = [.empty]
} else {
self?.cellTypes += Array(repeating: .location, count: self?.places.count ?? 0)
}
self?.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTypes.count
}
before appending .currentLocation everything was peachy. But now when I enter a text to search between locations, it crashes with Index Out Of Range error. I have tried to let place = places[indexPath.row + 1] and let place = places[indexPath.row - 1] but it was ineffective.
locationInteractor.didCurrrentPlaceLoaded = { [weak self] currentPlace in
self?.currentLocation = currentPlace
self?.cellTypes.append(.currentLocation)
self?.tableView.reloadData()
}
The problem was that my previous numberOfRowsInSection was as below.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellTypes.count
}
so this was causing me index out-of-range error.
After changing numberOfRowsInSection as below for each specific celltype my error is gone.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// sponsored by Serhat Sezen
if !places.isEmpty {
return places.count
} else {
return cellTypes.count
}
}