Didselect делегат табличного представления после проблемы поиска в swift
У меня есть контроллер представления и панель поиска над ним. эти данные, поступающие в табличном представлении, из службы. Я использовал фильтр поиска на нем. Элементы правильно ищутся, но когда я щелкаю ячейку, она открывает те же данные, что и перед поиском. Он снова и снова открывает одни и те же данные, а не открывает данные для этой фактической ячейки. Мой код для поиска в табличном представлении это,
var filteredNames = [PostCodeModel]()
var isFiltering = false
var dishNameArray = [String]()
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
isFiltering = true
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard let searchText = searchBar.text else {
isFiltering = false
return
}
filteredNames = PostCodeService.instance.postCodeModelInstance.filter({
return $0.name.lowercased().contains(searchText.lowercased())
})
isFiltering = filteredNames.count > 0
self.restaurantTableView.reloadData()
}
extension RestaurantVC: UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering {
return filteredNames.count
} else {
return PostCodeService.instance.postCodeModelInstance.count
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = restaurantTableView.dequeueReusableCell(withIdentifier: "restCell", for: indexPath) as! RestaurantListTableViewCell
if isFiltering {
cell.nameLbl.text = filteredNames[indexPath.row].name
cell.descriptionLbl.text = filteredNames[indexPath.row].description
cell.distanceLbl.text = filteredNames[indexPath.row].distance+"ml"
cell.minOrderLbl.text = String(filteredNames[indexPath.row].minimum_spending)
cell.deliveryLbl.text = String(filteredNames[indexPath.row].delivery_time) + "min"
cell.discountLbl.text = "20%off"
let nameImage = filteredNames[indexPath.row].icon_name
cell.restaurantImage.sd_setImage(with: URL(string: nameImage!), placeholderImage: UIImage(named: "placeholder.png"))
latitude = Double(filteredNames[indexPath.row].latitude)!
longitude = Double(filteredNames[indexPath.row].longitude)!
latitudeArray.append(latitude)
longitudeArray.append(longitude)
nameArray.append(cell.nameLbl.text!)
let defaults = UserDefaults.standard
defaults.set(latitudeArray, forKey: "latitudeArray")
defaults.set(longitudeArray, forKey: "longitudeArray")
defaults.set(nameArray, forKey: "nameArray")
defaults.synchronize()
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
return cell
} else {
cell.nameLbl.text = PostCodeService.instance.postCodeModelInstance[indexPath.row].name
cell.descriptionLbl.text = PostCodeService.instance.postCodeModelInstance[indexPath.row].description
cell.distanceLbl.text = PostCodeService.instance.postCodeModelInstance[indexPath.row].distance+"ml"
cell.minOrderLbl.text = String(PostCodeService.instance.postCodeModelInstance[indexPath.row].minimum_spending)
cell.deliveryLbl.text = String(PostCodeService.instance.postCodeModelInstance[indexPath.row].delivery_time) + "min"
cell.discountLbl.text = "20%"+"Off"
let nameImage = PostCodeService.instance.postCodeModelInstance[indexPath.row].icon_name
cell.restaurantImage.sd_setImage(with: URL(string: nameImage!), placeholderImage: UIImage(named: "placeholder.png"))
latitude = Double(PostCodeService.instance.postCodeModelInstance[indexPath.row].latitude)!
longitude = Double(PostCodeService.instance.postCodeModelInstance[indexPath.row].longitude)!
latitudeArray.append(latitude)
longitudeArray.append(longitude)
nameArray.append(cell.nameLbl.text!)
let defaults = UserDefaults.standard
defaults.set(latitudeArray, forKey: "latitudeArray")
defaults.set(longitudeArray, forKey: "longitudeArray")
defaults.set(nameArray, forKey: "nameArray")
defaults.synchronize()
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
return cell
}
}
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
filteredNames = PostCodeService.instance.postCodeModelInstance.filter({
return $0.name.lowercased().contains(searchText.lowercased())
})
restaurantTableView.reloadData()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if isFiltering {
let resId = String(filteredNames[indexPath.row].resId)
print(resId)
UserDefaults.standard.set(resId, forKey: "restaurantId")
UserDefaults.standard.set(resId, forKey: "resID")
UserDefaults.standard.synchronize()
let name = filteredNames[indexPath.row].name
let resImage = filteredNames[indexPath.row].icon_name
let description = filteredNames[indexPath.row].description
let minOrder = String(filteredNames[indexPath.row].minimum_spending)
let deliveryTime = String(filteredNames[indexPath.row].delivery_time)
UserDefaults.standard.set(name, forKey: "name")
UserDefaults.standard.set(description, forKey: "description")
UserDefaults.standard.set(minOrder, forKey: "minOrder")
UserDefaults.standard.set(minOrder, forKey: "deliveryTime")
UserDefaults.standard.set(resId, forKey: "resId")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RestaurantMenuVC") as! RestaurantMenuVC
vc.resId = resId
vc.name = name!
vc.restImage = resImage!
vc.resDesc = description!
vc.minOrder = minOrder
vc.deliveryTime = deliveryTime
self.navigationController?.pushViewController(vc, animated: true)
}
else
{
let resId = String(PostCodeService.instance.postCodeModelInstance[indexPath.row].resId)
print(resId)
UserDefaults.standard.set(resId, forKey: "restaurantId")
UserDefaults.standard.set(resId, forKey: "resID")
UserDefaults.standard.synchronize()
let name = PostCodeService.instance.postCodeModelInstance[indexPath.row].name
let resImage = PostCodeService.instance.postCodeModelInstance[indexPath.row].icon_name
let description = PostCodeService.instance.postCodeModelInstance[indexPath.row].description
let minOrder = String(PostCodeService.instance.postCodeModelInstance[indexPath.row].minimum_spending)
let deliveryTime = String(PostCodeService.instance.postCodeModelInstance[indexPath.row].delivery_time)
UserDefaults.standard.set(name, forKey: "name")
UserDefaults.standard.set(description, forKey: "description")
UserDefaults.standard.set(minOrder, forKey: "minOrder")
UserDefaults.standard.set(minOrder, forKey: "deliveryTime")
UserDefaults.standard.set(resId, forKey: "resId")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "RestaurantMenuVC") as! RestaurantMenuVC
vc.resId = resId
vc.name = name!
vc.restImage = resImage!
vc.resDesc = description!
vc.minOrder = minOrder
vc.deliveryTime = deliveryTime
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
Я разместил проверку для отфильтрованных данных, но она всегда показывает одни и те же результаты каждый раз, когда я нажимаю на результат поиска.