搜索的项目 returns 点击时详细视图错误
Searched item returns wrong detail view on click
我正在使用 Firebase
解析来自 JSON
的一些信息。我在 tableview
中实现了搜索控制器,它在搜索时 returns 纠正了项目,但问题是点击 (didSelectRowAtIndexPath
) 它 returns 错误的信息。是什么原因造成的,大家能告诉我吗?
这是我正在搜索的数组:
var brands = [String]()
这是过滤后的数组:
var filteredBrands = [String]()
这就是我在 didSelectRowAtIndexPath
:
中所做的
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedBrand = brands[indexPath.row]
performSegueWithIdentifier("toProducts", sender: self)
}
像这样传递选定的值:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "toProducts") {
let snusProductsView = segue.destinationViewController as! SnusProductsTableViewController
snusProductsView.brandName = selectedBrand
print(self.selectedBrand)
}
}
但由于某种原因,它传递了错误的值。它传递 tableView
中的第一项
我一定是傻了...发帖后我发现了问题所在。如果 resultSearchController
处于活动状态,我的 selectedBrand
是错误的。所以我这样修复它:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "toProducts") {
let snusProductsView = segue.destinationViewController as! SnusProductsTableViewController
snusProductsView.brandName = selectedBrand
print(self.selectedBrand)
}
}
在 didSelectRowAtIndexPath
中,你必须检查它是否是过滤版本,如果是这样,你需要使用 filteredBrands
而不是 brands
。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if isSearching {
selectedBrand = filteredBrands[indexPath.row]
} else {
selectedBrand = brands[indexPath.row]
}
我正在使用 Firebase
解析来自 JSON
的一些信息。我在 tableview
中实现了搜索控制器,它在搜索时 returns 纠正了项目,但问题是点击 (didSelectRowAtIndexPath
) 它 returns 错误的信息。是什么原因造成的,大家能告诉我吗?
这是我正在搜索的数组:
var brands = [String]()
这是过滤后的数组:
var filteredBrands = [String]()
这就是我在 didSelectRowAtIndexPath
:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedBrand = brands[indexPath.row]
performSegueWithIdentifier("toProducts", sender: self)
}
像这样传递选定的值:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "toProducts") {
let snusProductsView = segue.destinationViewController as! SnusProductsTableViewController
snusProductsView.brandName = selectedBrand
print(self.selectedBrand)
}
}
但由于某种原因,它传递了错误的值。它传递 tableView
我一定是傻了...发帖后我发现了问题所在。如果 resultSearchController
处于活动状态,我的 selectedBrand
是错误的。所以我这样修复它:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "toProducts") {
let snusProductsView = segue.destinationViewController as! SnusProductsTableViewController
snusProductsView.brandName = selectedBrand
print(self.selectedBrand)
}
}
在 didSelectRowAtIndexPath
中,你必须检查它是否是过滤版本,如果是这样,你需要使用 filteredBrands
而不是 brands
。
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if isSearching {
selectedBrand = filteredBrands[indexPath.row]
} else {
selectedBrand = brands[indexPath.row]
}