searchBar textDidChange不显示任何结果

时间:2017-05-16 01:59:29

标签: swift uitableview if-statement filter uisearchbar

我正在实现一个过滤tableView项的searchBar,并且在显示过滤数据时遇到了一些问题。应用程序运行且没有错误,但是当我键入searchBar时,tableview数据会隐藏,并且不会显示过滤的数据。我想根据存储在Firebase中的NSObject字符串值过滤tableView数据作为子值“place”。不确定textDidChange方法中有什么问题。提前感谢您的帮助!

// Firebase JSON
{
  "users" : {
    "CmtuebwVrmOG3n4uSsIK1b4FsSN2" : {
      "Places" : {
        "-KhDwZJvca9HedFluJ5O" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Edinburgh, UK",
          "timestamp" : 1.491678152020824E9
        },
        "-KhE7raDrM2RRs-N6FXg" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Hong Kong",
          "timestamp" : 1.49168137667081E9
        },
        "-KhFUaEjjhE3RBOw95fc" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Illinois, USA",
          "timestamp" : 1.491704112134812E9
        },
        "-Kk4EI1D7fuPyY2rvbdW" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Karnataka, India",
          "timestamp" : 1.494736515236516E9
        }
      },
    },

    lazy var searchBar:UISearchBar = UISearchBar()

    var isSearching = false
    var filteredData = [Place]()

    var placeList = [Place]()
    var placesDictionary = [String: Place]()

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.searchBarStyle = UISearchBarStyle.prominent
        searchBar.placeholder = " Search Places..."
        searchBar.sizeToFit()
        searchBar.isTranslucent = false
        searchBar.backgroundImage = UIImage()
        searchBar.delegate = self
        searchBar.returnKeyType = UIReturnKeyType.done
        navigationItem.titleView = searchBar

        fetchPlaces()
    }

    func fetchPlaces() {
        let uid = FIRAuth.auth()?.currentUser?.uid
        let ref = FIRDatabase.database().reference().child("users").child(uid!).child("Places")
        ref.observe(.childAdded, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {

                let place = Place()
                place.setValuesForKeys(dictionary)

                if let addedBy = place.addedBy {
                    self.placesDictionary[addedBy] = place
                    self.placeList.insert(place, at: 0)

                }

                //this will crash because of background thread, so lets call this on dispatch_async main thread
                DispatchQueue.main.async(execute: {
                    self.tableView.reloadData()
                })
            }

        }, withCancel: nil)

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)

        if isSearching {
            cell.textLabel?.text = filteredData[indexPath.row].place
        } else {

        cell.textLabel?.text = placeList[indexPath.row].place

        }

        return cell
    }

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

        if searchBar.text == nil || searchBar.text == "" {
            isSearching = false
            view.endEditing(true)
            tableView.reloadData()
        } else {
            isSearching = true
            filteredData = placeList.filter({$0.place == searchBar.text})
            tableView.reloadData()
        }
    } 

0 个答案:

没有答案