我会在这个前言中说我是Swift的新手。我有一个标签,显示从上一个视图控制器传递的用户输入变量。该表下方是一个tableview。目前,tableview正在显示我已硬编码到视图中的一系列城市。我希望根据标签中显示的变量过滤该tableview。即如果标签显示“拉斯维加斯”,我希望tableview只显示包含“拉斯维加斯”的行。过滤是我遇到问题的原因。这是我到目前为止所拥有的。
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 :(得分:0)
非常基本的骨架代码:
ejbModule
我在没有IDE的情况下对此进行了编码,可能存在一些语法错误,但基本上会根据更新textview时更新的某些filterText来更改您的tableview以进行过滤。