Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - Filter 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 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
}
like image 604
Jason Tremain Avatar asked Jun 26 '26 20:06

Jason Tremain


1 Answers

Pretty basic skeleton 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 may be a few syntax errors, but basically alter your tableview to filter based on some filterText that's updated whenever the textview is updated.

like image 69
Kevin DiTraglia Avatar answered Jun 29 '26 15:06

Kevin DiTraglia