2017-06-19 49 views
3

我會在序言中說我是Swift的新手。我有一個標籤,顯示從前一個視圖控制器傳遞的用戶輸入變量。在該表下方是桌面視圖。目前,tableview顯示了我已硬編碼到視圖中的一組城市。我希望根據標籤中顯示的變量對tableview進行過濾。即如果標籤顯示「Las Vegas」,我希望tableview只顯示包含「Las Vegas」的行。過濾是我有一個問題搞清楚。這是迄今爲止我所擁有的。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 
} 

回答

0

非常基本的骨架代碼:

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return cities.filter { $0.contains(self.filterText) }.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.filter { $0.contains(self.filterText) }[indexPath.row] 
    cell.cityName.text = labelCity 
    cell.stateName.text = labelState 
    cell.resultType.text = labelType 
    return(cell) 
} 

public func textViewDidChange(sender: UITextView, newText: String) { 
    self.filterText = newText 
    self.tableView.reloadData() 
} 

我編寫這個沒有一個IDE,有可能是一些語法錯誤,但基本上改變你的tableview過濾器的基礎上的一些filterText多數民衆贊成更新每當TextView的已更新。

+1

這會打電話過濾器很多。也許更好地緩存結果? –

+0

@TomHarrington可能不是基於OPs數據的大事,但是如果數據開始變大,可能值得一些緩存,也許不會在每個輸入的字符上調用完整的reloadData –

+0

謝謝你們,是的,這只是一個愚蠢的原型。不是真的用於生產應用程序。我只需要讓它對用戶測試起作用。 –