Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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()
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
251 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can zip (because you mentioned that the price array displays the prices that are correspondent to the menu array) two arrays, filter by menu values and get two filtered arrays.

let searchString = "Green"

let menu = ["Ice Tea (Large)", "Ice Tea (Small)", "Green Apple Refresher (Large)","Green Apple Refresher (Small)", "Peach Refresher (Large)", "Peach Refresher (Small)"]
let price = ["Rs.80", "Rs.50", "Rs.110", "Rs.80", "Rs.110", "Rs.80"]

let result = zip(menu, price).filter { menuItem, _ in
    menuItem.lowercased().contains(searchString.lowercased())
}

print(result) // array of tuples [("Green Apple Refresher (Large)", "Rs.110"), ("Green Apple Refresher (Small)", "Rs.80")]

let filteredMenu = result.map({ $0.0 })
let filteredPrice = result.map({ $0.1 })

print(filteredMenu, filteredPrice) // two separate arrays ["Green Apple Refresher (Large)", "Green Apple Refresher (Small)"] ["Rs.110", "Rs.80"]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...