首先声明,我对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
}
filter
。也许最好缓存结果? - Tom HarringtonString
?如果你想在第一个字符串上进行搜索,需要输入$0.0
,如果是第二个字符串,则输入$0.1
。如果你想搜索它们所有的字符串,你需要将其展开一些。 - Kevin DiTraglia