Victor
Victor

Reputation: 1623

Filter searchText to tableView

Right now I am using following

let data = ["New York, NY", "Los Angeles, CA" . . .]

var filteredData: [String]!

filteredData = data

But I want to use Firebase, with an almost identical structure, by using this

var data = [Categories]()

(This is categories)

struct Categories {

let key:String!
let content:String!
let itemRef:FIRDatabaseReference?

init (content:String, key:String = "") {
    self.key = key
    self.content = content
    self.itemRef = nil
}

init (snapshot:FIRDataSnapshot) {
    key = snapshot.key
    itemRef = snapshot.ref

    if let CategoriesContent = snapshot.value!["content"] as? String {
        content = CategoriesContent
    } else {
        content = ""
    }
}

}

So that when I search for something these lines is supposed to filter out everything that aren't correct

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    // Unhide tableview, this will be changed to another method
    tableView.hidden = false

    filteredData = searchText.isEmpty ? data : data.filter({(dataString: String) -> Bool in
        // If dataItem matches the searchText, return true to include it
        return dataString.rangeOfString(searchText) != nil
    })

    tableView.reloadData()
}

But since filter({(dataString: String) only takes strings it does not work

Question : Is there any other way to replace the string with my Firebase struct?

Thanks a lot!

Upvotes: 0

Views: 82

Answers (1)

Dikula
Dikula

Reputation: 21

this tutorial is so clear in UISearchResultsUpdating and Filtering section.

Upvotes: 1

Related Questions