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)
列表项
}) { (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)