2016-04-30 2 views
1

Я работаю над приложением IOS с помощью быстрой и firebase:Проблема с результатом поиска - Xcode проект

Я пытался добавить строку поиска в табличном виде следующего кода:

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() 
} 



} 

Вот Табличные: enter image description here

Когда пользователь щелкает на любом предмете, он retreives информации для этого элемента из firebase, если я нажал на Саре, например, это дает ей имя и ее электронную почту, как здесь: enter image description here

Когда я ищу, например, для Мэрайи или привет, это дает правильный результат, а здесь: enter image description here

Но если я нажал на результат пункта, он всегда получает информацию о первом пункте» Sara's Information ', например, результат mariah, дает следующее: enter image description here

Может ли кто-нибудь сказать мне, как я могу это исправить? и почему это происходит?

ответ

1

Я обнаружил, что вы не устанавливать обратный массив результата поиска в 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) 

      } 


     } 
1

В вашей cellForRow вы проверяете, если пользователь активно ищет и результат фильтрации

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if self.resultSearchController.active 
    { 


     if let name = self.filteredUsers[indexPath.row] as? NSMutableDictionary{ 
     // other code 
    } 
    else{ 
     ..// here get data from original array 

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


    return cell 
} 

Однако в то время как в didSelectRow методе вы не используете один и тот же поток

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


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

Это почему вы получаете неправильный результат. вам необходимо также проверить, если searchController активен здесь

if self.resultSearchController.active 
    { 
     //get from filteredUsers 
    } 
    else{ 
     //get from original users 
    } 
Смежные вопросы