Swift 3 - Filtering array based on string variable

I'll preface this saying: I'm new to Swift. I have a label that displays a user-entered variable that was passed from the previous view controller. Below this table is a tabular view. The tableview currently displays an array of cities which I have hardcoded into the view. I would like this table view to be filtered based on the variable displayed in the label. those. if the label displays Las Vegas, I want the table view to only display rows that contain Las Vegas. Filtering is what I'm having a problem with. 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
}

      

+3


source to share


1 answer


Pretty simple skeletal code:

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

      



I coded this without an IDE, there might be a few syntax errors, but basically change your tableview to filter based on some filterText that is updated whenever the textview is updated.

0


source







All Articles