Swift 3 - 根据字符串变量过滤数组

3

首先声明,我对Swift是新手。我有一个标签,显示了一个用户输入的变量,这个变量已经从上一个视图控制器传递过来。在该标签下方是一个表格视图。目前,表格视图正在显示我在视图中硬编码的城市数组。我希望该表格视图根据标签中显示的变量进行筛选。也就是说,如果标签显示“拉斯维加斯”,我希望表格视图仅显示包含“拉斯维加斯”的行。我遇到的问题是筛选。以下是我目前所拥有的内容。

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
}
1个回答

1

相当基础的骨架代码:

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()
}

我没有使用集成开发环境编写这段代码,可能会有一些语法错误,但基本上是将你的表视图更改为根据某个过滤文本进行筛选,该文本在每次更新文本视图时更新。


1
这会频繁调用 filter。也许最好缓存结果? - Tom Harrington
@TomHarrington 可能根据 OP 的数据来看不是什么大问题,但如果数据开始变得庞大,那么值得一些缓存,也许不需要在每个字符键入时调用完整的 reloadData。 - Kevin DiTraglia
谢谢大家,是的,这只是一个愚蠢的原型。不是真正的生产应用程序。我只需要让它在用户测试中运行即可。 - Jason Tremain
我从IDE收到了一个错误,上面写着“元组类型(String,String,String)的值没有成员'contains'。@KevinDiTraglia” - Jason Tremain
@JasonTremain 你想在元组中搜索哪个 String?如果你想在第一个字符串上进行搜索,需要输入 $0.0,如果是第二个字符串,则输入 $0.1。如果你想搜索它们所有的字符串,你需要将其展开一些。 - Kevin DiTraglia
明白了,谢谢,现在它可以工作了。感谢@KevinDiTraglia的帮助。 - Jason Tremain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接