私はそれぞれの国にポートのマッピングを保持する構造体オブジェクトを持っています。文字列と文字列の配列を含むstructオブジェクトのフィルタリング
struct countryAndPorts {
var country: String?
var ports: [String]?
}
私はすべての国のためにオブジェクトを保持する配列var countryAndPortsArray = [countryAndPorts]()
を持っています。
私も私の問題は、filteredArray
の結果である
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
}
この機能で、私はcountryAndPortsArray
をフィルタリングしています、ユーザーの検索クエリ
に応じてフィルタ処理countryAndPortsArray
オブジェクトを保持する配列var filteredArray = [countryAndPorts]()
を持っているが内のすべてのポートが含まれていますユーザーの検索キーに関係なく、同じ国が特定のポートに一致します。
たとえば、ユーザーが「パリ」を検索すると、filteredArray
にはフランスのすべてのポートが含まれます。ユーザーが「バルセロナ」を検索すると、filteredArray
にはスペインのすべてのポートが含まれます。
「barcelona」または「paris」と一致するポートだけが含まれ、同じ国の他のすべてのポートではありません。
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"]
を返すようにしたいと思う私のテーブルビューの代表者である、それは私の質問
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を更新しました –