xcode 表视图控制器搜索多个数组 [英] xcode table view controller search multiple arrays
问题描述
我添加了一个可以成功搜索菜单名称的搜索栏.但是,菜单价格根本没有过滤,我不知道如何控制这两个数组,当我搜索并获得 menu["a" , "c"] 的结果时,价格数组显示与菜单数组对应的价格;价格[a 的价格",c 的价格"].该列表显示三个对象;菜单图片、菜单名称和价格.
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()
}
}
推荐答案
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"]
问题是 menu
和 price
是同步的.这意味着如果您从 menu
中删除一项,则需要在 price
中删除相应的一项(相同的索引).但是让它们手动同步会增加工作要做,而对它们使用单个数组就足够了,而且更有意义.
The issue is that menu
and price
are sync'ed. Meaning that if you remove one item from menu
, you need to remove the corresponding one (same index) in price
. But keeping them manually sync'ed is adding work to do, while using a single array for them is enough and makes more sense.
快速解决方案:使用字典数组:
Quick solution: Use an array of Dictionaries:
var menu:[[String:String]] = ["Name":"Ice Tea (Large)", "Price":"Rs.80",
"Name":"Ice Tea (Small)", "Price":"Rs.50",
...]
那么:
cell.tdMenuImage.image = UIImage(named:currentArray[indexPath.row]["Name"] + ".jpg")
cell.tdMenuName.text = currentArray[indexPath.row]["Name"]
cell.tdMenuPrice.text = currentArray[indexPath.row]["Price"]
还有:
currentMenuNameArray = menu.filter( { (menu:[String:String]) -> Bool in
menu["Name"].lowercased().contains(searchText.lowercased())
})
我不知道你的代码的范围,但我建议创建你自己的对象/结构,带有一个字符串属性name
,一个双属性price
,可能是自定义枚举中的一个属性来定义它是大还是小.
I don't know the scope of your code, but I'd suggest to create your own object/struct, with a String Property name
, a Double property price
, maybe a property from a custom enum to define if it's large or small.
这不是漂亮的 Swift 代码,但更多的是解释逻辑,以及使用自定义对象能给您带来什么好处:
It's not beautiful Swift code, but it's more to explain the logic, and what could give you the benefits of using custom objects:
import UIKit
enum MenuSize: String {
case small = "Small"
case large = "Large"
}
class Menu: NSObject {
let name: String
let price: Double
let size: MenuSize
required init(withName name: String, withPrice price: Double, andSize size: MenuSize) {
self.name = name
self.price = price
self.size = size
}
func displayableName() -> String {
return name + " (" + self.size.rawValue + ")"
}
func imageName() -> String {
return self.name + ".jpg"
}
}
要测试的示例代码:
let menu1 = Menu.init(withName: "Ice tea", withPrice: 0.80, andSize: .large)
let menu1Name = menu1.displayableName()
print("menu1Name: \(menu1Name)")
let menu2 = Menu.init(withName: "Ice tea", withPrice: 0.50, andSize: .small)
let menu3 = Menu.init(withName: "Diet Coke", withPrice: 0.50, andSize: .small)
let menu4 = Menu.init(withName: "Diet Coke", withPrice: 1.00, andSize: .large)
let array = [menu1, menu2, menu3, menu4]
let filtered = array.filter { (menuToTest:Menu) -> Bool in
return menuToTest.name.lowercased().contains("et".lowercased())
}
您可以使用 NumberFormatter
以卢比显示价格.(相关 SO 问题).
You could use a NumberFormatter
to display the price with Rupees. (Related SO question).
tableView(_ tableView:cellForRowAt:)
中的代码就是:
let currentMenu = currentArray[indexPath.row]
cell.tdMenuImage.image = UIImage(named: currentMenu.imageName())
cell.tdMenuName.text = currentMenu.displayableName()
cell.tdMenuPrice.text = //Use the formatter
这篇关于xcode 表视图控制器搜索多个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!