I'll preface this saying I'm new to Swift. I have a label that displays a user entered variable that has been passed from the previous view controller. Below that table is a tableview. Currently the tableview is displaying an array of cities that I've hardcoded into the view. I'd like that tableview to be filtered based on the variable that is displayed in the label. i.e. if the label shows "Las Vegas", I want the tableview to only display rows that contain "Las Vegas". Filtering is what I'm having a problem figuring out. Here's what I have so far.
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
}