如何在Firebase Database Swift中搜索

时间:2018-11-16 13:24:36

标签: swift firebase search firebase-realtime-database uisearchbar

am试图在我的Firebase数据库中使用UISearchbar,但是我不知道我的代码是否为rghit,因为我每次运行它时都会崩溃,这是我的代码

var hotelList = [hotelsModel]()
var hotelsArray : [NSDictionary] = []
var filterHotels : [NSDictionary] = []
var databaseRef = Database.database().reference()


databaseRef.child("Hotels").queryOrdered(byChild: "name").observe(.childAdded, with: { (snapshot) in
        self.hotelsArray.append(snapshot.value as! NSDictionary)

        self.tableView.insertRows(at: [IndexPath(row: self.hotelsArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic)
  1. 列表项

    }) { (error) in
        print(error.localizedDescription)
    }
    

    func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {     //使用自定义类创建单元     让cell = tableView.dequeueReusableCell(withIdentifier:“ cell”,for:indexPath)为! hotelsTableViewCell

    let hotelSearch : NSDictionary?
    //the artist object
    var hotel: hotelsModel
    //getting the artist of selected position
    //hotel = hotelList[indexPath.row]
    if searchController.isActive && searchController.searchBar.text != "" {
    
        hotelSearch = filterHotels[indexPath.row] as! NSDictionary
    } else {
        //getting the artist of selected position
        hotel = hotelList[indexPath.row]
    }
    
    //getting the artist of selected position
    hotel = hotelList[indexPath.row]
    //adding values to labels
    cell.lblName.text = hotel.name
    cell.lblLocation.text = hotel.location
    cell.appSuggest.text = hotel.appSuggest
    cell.price.text = hotel.price
    cell.canceletion.text = hotel.cancelation
    cell.paymentNeeded.text = hotel.paymentNeeded
    
    if let imgUrl = hotel.img{
        let url = URL(string: imgUrl)
        cell.img.kf.setImage(with: url)
        cell.img.clipsToBounds = true
        cell.img.layer.cornerRadius = 10
        cell.img.layer.shadowColor = UIColor.black.cgColor
        cell.img.layer.shadowOpacity = 1.8
        cell.img.layer.shadowOffset = CGSize(width: 5, height: 0)
    }
          return cell
    

    } func updateSearchResults(用于searchController:UISearchController){     //更新搜索栏结果     //filterHotels(serachText:searchController.searchBar.text!)     filterHotels.removeAll()     filterHotels = hotelsArray.filter({(text)-> Bool in         让tmp:NSString =(text as?NSString ?? nil)?? ”         让范围= tmp.range(of:searchController.searchBar.text !,选项:NSString.CompareOptions.caseInsensitive)         返回range.location!= NSNotFound     })     self.tableView.reloadData() }

它在此行中崩溃了

self.hotelsArray.append(snapshot.value as! NSDictionary)

我该如何解决?

谢谢

0 个答案:

没有答案