2017-06-19 50 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

谢谢你们,是的,这只是一个愚蠢的原型。不是真的用于生产应用程序。我只需要让它对用户测试起作用。 –