gpt4 book ai didi

ios - 搜索结果问题 - Xcode 项目

转载 作者:行者123 更新时间:2023-12-01 20:15:04 25 4
gpt4 key购买 nike

我正在使用 swift 和 firebase 开发 iOS 应用程序:
我尝试将搜索栏添加到表格 View 中,如下代码:

import UIKit

class MyTableVC: UITableViewController,UISearchResultsUpdating {

var resultSearchController = UISearchController(searchResultsController: nil)
//var filteredUsers = [nsdi]()

var filteredUsers: NSMutableArray = []
var UserNamesArray: NSMutableArray = []


override func viewDidLoad() {
super.viewDidLoad()

// self.resultSearchController = UISearchController(searchResultsController: nil)
// self.resultSearchController.searchResultsUpdater = self
// self.resultSearchController.dimsBackgroundDuringPresentation = false
// //self.resultSearchController.searchBar.sizeThatFits()


self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()

self.tableView.tableHeaderView = controller.searchBar

return controller
})()


self.tableView.tableHeaderView = self.resultSearchController.searchBar

self.tableView.reloadData()


let ref = Firebase(url: "https://hissah-1st-fb-test.firebaseio.com/Users")

ref.queryOrderedByChild("Job").queryEqualToValue("Programs")
.observeEventType(.Value, withBlock: { snapshot in

if let dict = snapshot.value as? NSMutableDictionary{
//print("dict====== \(dict)")

for (key,value) in dict {
let mainDict = NSMutableDictionary()
mainDict.setObject(key, forKey: "userid")

if let dictnew = value as? NSMutableDictionary{

if let metname = dictnew["UserName"] as? String
{
mainDict.setObject(metname, forKey: "UserName")
}
if let metname = dictnew["Details"] as? String
{
mainDict.setObject(metname, forKey: "Details")
}
if let metname = dictnew["Email"] as? String
{
mainDict.setObject(metname, forKey: "Email")
}
}

//print("mainDict========= \(mainDict)")

self.UserNamesArray.addObject(mainDict)

}
//print("UserNamesArray ==== \(self.UserNamesArray)")
}
self.tableView.reloadData()

})
}





// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.resultSearchController.active
{
return self.filteredUsers.count
}else
{
return UserNamesArray.count
}
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UserCell", forIndexPath: indexPath) as UITableViewCell

if self.resultSearchController.active
{
// cell.textLabel?.text = self.filteredUsers[indexPath.row] as? String


if let name = self.filteredUsers[indexPath.row] as? NSMutableDictionary{

cell.textLabel?.text = name["UserName"] as? String
cell.detailTextLabel?.text = name["Details"] as? String
}


}
else{


if let name = UserNamesArray[indexPath.row] as? NSMutableDictionary{

cell.textLabel?.text = name["UserName"] as? String
cell.detailTextLabel?.text = name["Details"] as? String
}
}
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator

return cell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

tableView.deselectRowAtIndexPath(indexPath, animated: true)
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
let detailsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("DetailsViewController") as! DetailsVC

if let name = self.UserNamesArray[indexPath.row] as? NSMutableDictionary{

detailsViewController.self.strUserid = name["userid"] as? String
}
self.navigationController?.pushViewController(detailsViewController, animated: true)
}
}

func updateSearchResultsForSearchController(searchController: UISearchController)
{
filteredUsers.removeAllObjects()

//attributeA contains[cd] $A OR attributeB contains[cd]
let searchPredicate = NSPredicate(format: "UserName contains[cd] %@ OR Details contains[cd] %@", searchController.searchBar.text!,searchController.searchBar.text!)
let array = (UserNamesArray as NSArray).filteredArrayUsingPredicate(searchPredicate)
for type in array {
// Do something

filteredUsers .addObject(type)
}

// filteredUsers = array as! [String]

self.tableView.reloadData()
}



}
这是表格 View :
enter image description here
当用户单击任何项​​目时,它会从 firebase 检索该项目的信息,例如,如果我单击 Sara,它会给出她的姓名和电子邮件,如下所示:
enter image description here
例如,当我搜索 mariah 或 hello 时,它会给出正确的结果,如下所示:
enter image description here
但是如果我点击结果项目,它总是检索第一个项目的信息!'Sara's Information',例如结果 mariah,给出了这个:
enter image description here
谁能告诉我,我该如何解决这个问题?为什么会这样?

最佳答案

我发现您没有在 didSelectRowAtIndexPath 处设置搜索结果的返回数组.所以你设置didSelectRowAtIndexPath代码如下:

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


if self.resultSearchController.active
{

tableView.deselectRowAtIndexPath(indexPath, animated: true)
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
let detailsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("DetailsViewControllerroller") as! DetailsViewController

if let name = self.filteredUsers[indexPath.row] as? NSMutableDictionary{

detailsViewController.self.strUserid = name["userid"] as? String
}

self.navigationController?.pushViewController(detailsViewController, animated: true)

}

}
else
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
let detailsViewController = self.storyboard!.instantiateViewControllerWithIdentifier("DetailsViewControllerroller") as! DetailsViewController

if let name = self.UserNamesArray[indexPath.row] as? NSMutableDictionary{

detailsViewController.self.strUserid = name["userid"] as? String
}

self.navigationController?.pushViewController(detailsViewController, animated: true)

}


}

关于ios - 搜索结果问题 - Xcode 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com