我有一个结构对象,它持有端口到其各自国家的映射。过滤包含字符串和字符串数组的结构对象
struct countryAndPorts {
var country: String?
var ports: [String]?
}
我有一个适用于每个国家对象数组var countryAndPortsArray = [countryAndPorts]()
。
我也有一个数组var filteredArray = [countryAndPorts]()
持有过滤取决于用户的搜索查询
我与这个功能过滤countryAndPortsArray
countryAndPortsArray
对象
func filteredContent(searchKey: String) {
filteredArray = countryAndPortsArray.filter {
!($0.ports?.filter{
$0.range(of: searchKey, options: .caseInsensitive, range:
nil, locale: nil) != nil ||
$0.localizedCaseInsensitiveCompare(searchKey) == .orderedSame
}.isEmpty ?? true) ||
$0.country?.localizedCaseInsensitiveContains(searchKey) ?? true
}
我的问题是filteredArray
结果包含内的每一个端口无论用户的搜索关键字是否与特定端口匹配,都是同一个国家。
例如,如果用户搜索“巴黎”,filteredArray
将包含法国的每个端口。如果用户搜索“巴塞罗那”,filteredArray
将包含西班牙的每个港口。
期望的结果应该只包含匹配“巴塞罗那”或“巴黎”的端口,而不是同一个国家内的所有其他端口。
UPDATE
使用案例:
var france = countryAndPorts(country: "France", ports:
["monaco","paris","ajaccio","lyon"])
var spain = countryAndPorts(country: "Spain", ports: ["barcelona",
"madrid", "catalan"])
var countryAndPortsArray = [france,spain]
如果我搜索countryAndPortsArray
巴塞罗那,它返回["barcelona", "madrid", "catalan"]
。但是我希望它只是返回["barcelona"]
,这里是我的tableview代表只是柜面它揭示更多的光线,我的问题
extension SearchDealsViewController: UITableViewDelegate,
UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section:
Int) -> Int {
if let count = filteredArray[section].ports?.count {
return count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for:
indexPath)
if let port = filteredArray[indexPath.section].ports?
[indexPath.row] {
cell.textLabel?.text = port
}
cell.textLabel?.font = UIFont.systemFont(ofSize: 10)
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return filteredArray.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
portsFromTextField.text = filteredArray[indexPath.section].ports?
[indexPath.row]
}
func tableView(_ tableView: UITableView, viewForHeaderInSection
section: Int) -> UIView? {
let header = tableView.dequeueReusableCell(withIdentifier:
"header")
let tapSectionHeaderGesture = UITapGestureRecognizer(target: self,
action: #selector(getCountryText(sender:)))
tapSectionHeaderGesture.numberOfTouchesRequired = 1
tapSectionHeaderGesture.numberOfTapsRequired = 1
header?.addGestureRecognizer(tapSectionHeaderGesture)
if header != nil {
header?.textLabel?.textColor = THEME_COLOUR
header?.textLabel?.font = UIFont.boldSystemFont(ofSize: 12)
header?.textLabel?.text = filteredArray[section].country
}
return header
}
func getCountryText(sender: UITapGestureRecognizer) {
if let country = sender.view as? UITableViewCell {
portsFromTextField.text = country.textLabel?.text
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection
section: Int) -> CGFloat {
return 30
}
你能不能提供期望的结果小数据的例子。 –
@OlegGordiichuk更新 –