I am parsing some info from JSON
using Firebase
. I implemented search controller into the tableview
, it returns correct items while searching but the problem is that on click (didSelectRowAtIndexPath
) it returns wrong info. What could cause this, can you tell me guys?
This is the arrays in which I am searching:
var brands = [String]()
And this is the filtered array:
var filteredBrands = [String]()
And this is what I am doing inside the didSelectRowAtIndexPath
:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedBrand = brands[indexPath.row]
performSegueWithIdentifier("toProducts", sender: self)
}
And like this I pass the selected value:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "toProducts") {
let snusProductsView = segue.destinationViewController as! SnusProductsTableViewController
snusProductsView.brandName = selectedBrand
print(self.selectedBrand)
}
}
But for some reason it passes the wrong value. It passes the first item inside tableView
Inside didSelectRowAtIndexPath
you have to check is it filtered version or not if so you need to use filteredBrands
instead of brands
.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if isSearching {
selectedBrand = filteredBrands[indexPath.row]
} else {
selectedBrand = brands[indexPath.row]
}