I've added a search bar that successfully searches through the menu names. However, the menu price does not filter at all, and I don't know how to control the two arrays in a way that when I search and get a result of menu["a" , "c"], the price array displays the prices that are correspondant to the menu array; price["Price of a", "Price of c"]. The list displays three objects; image of the menu, name of the menu, and the price.
var menu = ["Ice Tea (Large)", "Ice Tea (Small)", "Green Apple Refresher (Large)","Green Apple Refresher (Small)", "Peach Refresher (Large)", "Peach Refresher (Small)"]
var price = ["Rs.80", "Rs.50", "Rs.110", "Rs.80", "Rs.110", "Rs.80"]
var currentMenuNameArray = [String]()
class myMenu: UITableViewController, UISearchBarDelegate
{
@IBOutlet weak var tdMenuSearchBar: UISearchBar!
override func viewDidLoad()
{
super.viewDidLoad()
tdMenuSearchBar.delegate = self
currentMenuNameArray = menu
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return currentMenuNameArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as!TigersDenTableViewCell
cell.tdMenuImage.image = UIImage(named:currentMenuNameArray[indexPath.row] + ".jpg")
cell.tdMenuName.text = currentMenuNameArray[indexPath.row]
cell.tdMenuPrice.text = price[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
guard !searchText.isEmpty else {
currentMenuNameArray = menu
tableView.reloadData()
return
}
currentMenuNameArray = menu.filter( { (menu:String) -> Bool in menu.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
}