Ios 基于字符串变量的swift3滤波器阵列

Ios 基于字符串变量的swift3滤波器阵列,ios,swift,uitableview,swift3,filtering,Ios,Swift,Uitableview,Swift3,Filtering,我会在这句话的开头说我是斯威夫特的新手。我有一个标签,显示从上一个视图控制器传递的用户输入变量。在那张桌子下面是一个tableview。目前,tableview显示了我硬编码到视图中的一系列城市。我希望根据标签中显示的变量筛选tableview。i、 e.如果标签显示“Las Vegas”,我希望tableview仅显示包含“Las Vegas”的行。过滤是我发现的一个问题。这是我到目前为止所拥有的 import UIKit class ResultsViewController: UIVie

我会在这句话的开头说我是斯威夫特的新手。我有一个标签,显示从上一个视图控制器传递的用户输入变量。在那张桌子下面是一个tableview。目前,tableview显示了我硬编码到视图中的一系列城市。我希望根据标签中显示的变量筛选tableview。i、 e.如果标签显示“Las Vegas”,我希望tableview仅显示包含“Las Vegas”的行。过滤是我发现的一个问题。这是我到目前为止所拥有的

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
}

基本框架代码:

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进行筛选,该filterText会在textview更新时进行更新。

这将调用
filter
。也许缓存结果更好?@TomHarrington基于OPs数据可能没什么大不了的,但如果数据开始变大,可能值得缓存,也许不需要对每个字符类型调用完全重载数据Hanks伙计们,是的,这只是一个愚蠢的原型。不适用于生产应用程序。我只需要让它在用户测试中起作用。我从IDE中得到一个错误,它说“元组类型的值(字符串,字符串,字符串)没有成员“包含”@KevinDiTraglia@JasonTremain要搜索元组中的哪个
String
?如果要在第一个字符串
$0.1
中搜索第二个字符串,则需要它说出类似
$0.0
的内容。如果要搜索所有字符串,则必须将其进一步展开。