【发布时间】:2017-04-26 09:58:30
【问题描述】:
我有一个结构对象,其中包含端口到各自国家/地区的映射。
struct countryAndPorts {
var country: String?
var ports: [String]?
}
我有一个数组 var countryAndPortsArray = [countryAndPorts]() 包含每个国家/地区的 countryAndPort 对象。
我还有一个数组 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 的结果包含同一国家/地区内的每个端口,而不管用户的搜索关键字是否匹配特定端口。
例如,如果用户搜索“Paris”,filteredArray 将包含法国的每个港口。如果用户搜索“barcelona”,filteredArray 将包含西班牙的每个港口。
所需结果应仅包含与“barcelona”或“paris”匹配的端口,而不应包含同一国家/地区的所有其他端口。
更新
用例:
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 已更新