我有一个搜索栏,可用于搜索控件段(所有商店)中的任何项目。并控制段过滤商店。
所以:
搜索所有商店
细分市场是过滤商店
以下是我保存所选索引的方法
s.valueChange = { index in
switch index {
case 0 :
self.selectedIndex = 0
self.tableView.reloadData()
case 1 :
self.selectedIndex = 1
self.tableView.reloadData()
case 2 :
self.selectedIndex = 2
self.tableView.reloadData()
case 3 :
self.selectedIndex = 3
self.tableView.reloadData()
default:
self.selectedIndex = 0
self.tableView.reloadData()
}
这是
部分的行数func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searchActive {
return auxiliar.count
} else {
switch selectedIndex {
case 0:
return allShops.count
case 1:
return self.coffee.count
case 2:
return self.restuarnt.count
case 3:
return self.pharmacy.count
default:
return allShops.count
}
}
}
func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {
if searchActive
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
let entry1 = auxiliar[indexPath.row]
cell.shopName.text = entry1.shopname
return cell
} else {
switch selectedIndex {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
let entry = shops[indexPath.row]
cell.shopName.text = entry.shopname
return cell
case 1 :
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
let entry = coffee[indexPath.row]
cell.shopName.text = entry.shopname
return cell
case 2 :
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
let entry = restuarnt[indexPath.row]
cell.shopName.text = entry.shopname
return cell
case 3 :
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
let entry = pharmacy[indexPath.row]
cell.shopName.text = entry.shopname
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ShopTableViewCell
let entry = shops[indexPath.row]
cell.shopName.text = entry.shopname
return cell
}
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if searchActive {
guard (auxiliar.count) > indexPath.row else {
print("Index out of range")
return
}
let meal1 = auxiliar[indexPath.row]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController
viewController.passedValue = (meal1.familiy_id)
viewController.name = (meal1.shopname)
print("\(meal1.familiy_id) im the search ")
view.animateRandom()
self.present(viewController, animated: true , completion: nil)
} else {
switch selectedIndex {
case 0 :
let meal1 = shops[indexPath.row]
guard (shops.count) > indexPath.row else {
print("Index out of range")
return
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController = storyboard.instantiateViewController(withIdentifier: "viewControllerIdentifer") as! MealsDetailsController
viewController.passedValue = (meal1.familiy_id)
viewController.name = (meal1.shopname)
print("\(meal1.familiy_id) im the search ")
view.animateRandom()
self.present(viewController, animated: true , completion: nil)
case 1 :
let meal1 = coffee[indexPath.row]
guard (coffee.count) > indexPath.row else {
print("Index out of range")
return
}
... the rest of the cases
最后是textDidChange
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
auxiliar = shops.filter { $0.shopname.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil}
if searchText == "" || searchBar.text == nil {
auxiliar = shops
}
tableView.reloadData()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
{
self.searchActive = true
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar)
{
self.searchActive = true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
self.searchActive = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
self.searchActive = false
}
func searchBarBookmarkButtonClicked(_ searchBar: UISearchBar) {
self.searchActive = false
}
问题
当我搜索商店时,结果在所有细分中都很好。但当我结束搜索并更改片段后,搜索后的所有片段都有段0(selectedIndex = 0)的行,并且再也不会更改
当我点击搜索栏内的X按钮时,应用程序崩溃
我想要的是什么:
在结束搜索和搜索栏为“”或nill之后,我想点击任何细分,并显示属于该细分的商店。