3
私はSwiftを初めて使っていると言っています。私は、前のView Controllerから渡されたユーザー入力変数を表示するラベルを持っています。そのテーブルの下にはテーブルビューがあります。現在、テーブルビューには、ビューにハードコードされた都市の配列が表示されています。私はそのテーブルビューは、ラベルに表示される変数に基づいてフィルタリングしたいと思います。ラベルに「ラスベガス」と表示されている場合は、「ラスベガス」を含む行のみを表示するようにtableviewを設定します。フィルタリングは私が問題を理解しているものです。ここに私がこれまで持っているものがあります。Swift 3 - 文字列変数に基づくフィルタ配列
import UIKit
class ResultsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let cities = [
("Orlando", "FL, United States", "Location"),
("Orlando", "AR, United States", "Location"),
("Orlando", "KY, United States", "Location"),
("Orlando", "NC, United States", "Location"),
("Orlando", "OK, United States", "Location"),
("Orlando", "NY, United States", "Location"),
("Orlando", "VA, United States", "Location"),
("Orlando", "WV, United States", "Location"),
("Las Vegas", "NV, United States", "Location"),
("Las Vegas", "TX, United States", "Location"),
("Las Vegas", "NM, United States", "Location"),
("Scottsdale", "AZ, United States", "Location"),
("Scottsdale Plaza", "PA, United States", "Location"),
("Scottsdale Pond", "CA, United States", "Location"),
("Scottsdale Park", "IL, United States", "Location")]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(cities.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ResultsControllerTableViewCell
let (labelCity, labelState, labelType) = cities[indexPath.row]
cell.cityName.text = labelCity
cell.stateName.text = labelState
cell.resultType.text = labelType
return(cell)
}
@IBOutlet weak var userSearchInputLabel: UILabel!
var searchItem = String()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
userSearchInputLabel.text = searchItem
self.extendedLayoutIncludesOpaqueBars=true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.navigationItem.hidesBackButton = true
}
これは 'filter'をたくさん呼び出すことになるでしょう。結果をキャッシュする方が良いでしょうか? –
@TomHarringtonはおそらくOPデータに基づいて大したことではありませんが、データが大きくなるとキャッシングの価値があり、タイプされたすべての文字に対して完全なreloadDataを呼び出さない場合があります –
おかげで、プロトタイプ。実用アプリケーションではありません。私は、ユーザーテストのために機能するようにする必要があります。 –