3

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
}

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

That's going to call filter a lot. Maybe better to cache the result?
@TomHarrington probably not a big deal based on OPs data, but yeah if the data starts to get large it may be worth some caching and maybe not calling a full reloadData on every character typed
Thanks guys, yeah this is just for a dumb prototype. Not really for a production application. I just need to get it to be functional for user testing.
I'm getting an error from the IDE saying "Value of tuple type (String, String, String) has no member 'contains'. @KevinDiTraglia
@JasonTremain Which String in the Tuple do you want to search on? You'll need it to say something like $0.0 if you want to search on the first string $0.1 for the second. If you want to search them all you'll have to expand it out a little more.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.