は、基本的にこれは、これらがそのポートにマッピングされたそれぞれの国を保持している配列である構造体の国を保持するオブジェクトとそのポート文字列と配列を含む構造体をフィルタリングする方法は?
struct countryAndPorts {
var country: String?
var ports: [String]?
}
あります。フィルタリングされた配列は、フィルタリングされた結果を国別またはポート別に保持する必要があります。
var countryAndPortsArray = [countryAndPorts]()
var filteredArray = [countryAndPorts]()
現在、以下の機能が正常に検索キー
func filteredContent(searchKey: String) {
filteredArray = countryAndPortsArray.flatMap{ $0 }.filter{$0.country?.lowercased().range(of: searchKey) != nil }
}
として国を使用してフィルタリングされた結果を生成します今私の問題は、私は、検索キーとして、ポートまたは国のいずれかを使用してフィルタリングされた結果を取得したいです。私は可能な限りベストを尽くしましたが、検索キーとしてポートを使って望ましい結果を得られないようです。私の現在の方法の改善は非常に高く評価されるだろう。 Objective-Cの回答も歓迎されます。
//Just incase it's of any use, these are my UITableView delegates
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
}
-------------------------------- UDPATE --------- ----------------------
現在、検索文字列として「vlone」と入力すると、「vlone」と「vlore」の両方が返されます。 Vloneとヴロラは、所望の出力のみが「vlone」が含まれ、ユーザが自分のクエリで特定したので、アルバニア内の他のすべてのポートを除外すべきであるしかしアルバニア
で両方のポートResult yielded using vlone as search string
です。
第2のシナリオでは、「バルセロナ」を検索キーとして使用します。スペインのすべてのポートを含む結果が返されますが、目的の出力にはバルセロナのみが含まれている必要があります。
Results using barcelona as search key
なぜマッピングの構造体を定義しますか? 'var countriesAndPorts = [String:[String]]()'を使い、 'let ports = countriesAndPorts [" Spain "]'を呼んで国のポートを取得してください。 – NRitH
@NRitH辞書より構造が優れています。他のプロパティがあるかもしれませんが、追加できるロジックがあるかもしれません。 – rmaddy
@NRitHオンラインでどこかを見ましたが、それほど考えられませんでした。通常、私は構造体を使用して、クラスではなく複雑なデータ構造を構築します。 –