2017-08-25 64 views
0
struct postStruct { 
let title : String! 
let author : String! 
let bookRefCode : String! 
let imageDownloadString : String! 
let status : String! 
let reserved : String! 
let category : String! 
let dueDate : String! 
} 

'上面是我为帖子设置结构的位置,下面是我如何引用和检索Firebase数据库中的数据。UISearchBar和Firebase数据库

我的问题是,当你设置搜索者时,我不知道如何根据帖子的标题来搜索它。

class DirectoryTableView: UITableViewController { 

var posts = [postStruct]() 

override func viewDidLoad() { 

    let databaseRef = Database.database().reference() 

    databaseRef.child("Books").queryOrderedByKey().observe(.childAdded, with: { 
     snapshot in 

     var snapshotValue = snapshot.value as? NSDictionary 
     let title = snapshotValue!["title"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 

     let author = snapshotValue!["author"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 

     let bookRefCode = snapshotValue!["bookRefCode"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 

     let status = snapshotValue!["status"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 

     let reserved = snapshotValue!["reserved"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 

     let category = snapshotValue!["category"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 

     let dueDate = snapshotValue!["dueDate"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 


     self.posts.insert(postStruct(title: title, author: author, bookRefCode: bookRefCode, status: status, reserved: reserved, category: category, dueDate: dueDate) , at: 0) 
     self.tableView.reloadData() 


    }) 



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return posts.count 

} 




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

    var cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

    let databaseRef = Database.database().reference() 

    let label1 = cell?.viewWithTag(1) as! UILabel 
    label1.text = posts[indexPath.row].title 

    let label2 = cell?.viewWithTag(2) as! UILabel 
    label2.text = posts[indexPath.row].author 

    let label3 = cell?.viewWithTag(3) as! UILabel 
    label3.text = posts[indexPath.row].bookRefCode 

    let label4 = cell?.viewWithTag(4) as! UILabel 
    label4.text = posts[indexPath.row].status 

    let label5 = cell?.viewWithTag(5) as! UILabel 
    label5.text = posts[indexPath.row].category 

    let image1 = cell?.viewWithTag(6) as! UILabel 
    image1.text = posts[indexPath.row].imageDownloadString 

    let label6 = cell?.viewWithTag(7) as! UILabel 
    label6.text = posts[indexPath.row].reserved 

    let label9 = cell?.viewWithTag(9) as! UILabel 
    label9.text = posts[indexPath.row].dueDate 

    return cell! 

} 

“此外,没有人知道如何实现代码如下细胞(在这种情况下帖)按字母顺序排序?

回答

0

你可以得到所有数据已按字母顺序排列

databaseRef.child("Books").queryOrdered(byChild: "title").observe(.childAdded, with: { snapshot in 

    var snapshotValue = snapshot.value as? NSDictionary 
     let title = snapshotValue!["title"] as? String 
     snapshotValue = snapshot.value as? NSDictionary 

    .... 

} 

或之前整理你的阵列重新加载tableView

var sortedArray = swiftArray.sorted { $0.title.localizedCaseInsensitiveCompare($1.title) == ComparisonResult.orderedAscending } 

样品结构

enter image description here

+0

我不排序 – joeyhal

+0

我编辑了我的答案。只需将'.observe(.value,'改为'.observe(.childAdded')。我在这里测试过,它对我有用 – kileros

+0

@kileros我们可以查询,就好像我的数据库任何孩子在标题词中都有字母“a”所以得到它snapShot?像“appcoda”有“一个”在它和更多的标题是这样吗? –

0

根据整理数据searchBar我h广告中使用的是有我的所有快照的字典和我相比,在字典之后我搜索栏文本排序重载的tableView这里是代码,如果你使用第一种方法,你可以看看

//method to get all user Details in a dict 

func getEmail() { 

    let databaseRef = Database.database().reference().child("users") 

    databaseRef.observe(.value, with: { (snapshot) in 
     if snapshot.exists(){ 
      self.postData = snapshot.value as! [String : AnyObject] 

      let dictValues = [AnyObject](self.postData.values) 
      self.sarchDict = dictValues 

     } 
    }) 
} 
//search bar delegate 
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 

if self.mySearchBar.text!.isEmpty { 

// set searching false 
self.isSearching = false 

}else{ 

// set searghing true 
self.isSearching = true 

self.names.removeAll() 
self.uidArray.removeAll() 
self.imageUrl.removeAll() 

for key in self.sarchDict { 

let mainKey = key 
//I am making query against email in snapshot dict 


let str = key["email"] as? String 

    //taking value of email from my dict lowerCased to make query as case insensitive 

let lowercaseString = str?.lowercased() 
        //checking do my any email have entered letter or not 
if(lowercaseString?.hasPrefix(self.mySearchBar.text!.lowercased()))!{ 

//here I have a check so to remove value of current logged user 
if ((key["uID"] as! String) != (Auth.auth().currentUser?.uid)!){ 

//If value is found append it in some arrays 
self.imageUrl.append(key["profilePic"] as! String) 
self.names.append(key["name"] as! String) 
self.uidArray.append(key["uID"] as! String) 

//you can check which values are being added from which key 
print(mainKey) 

} 
} 
} 
//reload TableView here 
}  
} 

//TableView 

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

    cell = self.myTableView.dequeueReusableCell(withIdentifier: "Cell")! 


    if self.isSearching == true { 

     let imageView = (cell.viewWithTag(1) as! UIImageView) 
     imageView.setRounded() 
     if imageUrl[indexPath.row] != "" { 
      self.lazyImage.showWithSpinner(imageView:imageView, url:imageUrl[indexPath.row]) 
     } 
     else{ 
      imageView.image = UIImage(named: "anonymous") 
     } 


     (cell.contentView.viewWithTag(2) as! UILabel).text = self.names[indexPath.row] 

    } 
    else { 


    } 

    return cell 
}