swift - UISearchBar 和 Firebase 数据库

标签 swift uitableview sorting firebase uisearchbar

 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 数据库引用和检索数据的地方。

我的问题是,当您设置搜索器时,我不知道如何让它根据帖子标题进行搜索。'

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!

}

“另外,有谁知道如何按字母顺序对表格 View 单元格(在本例中为帖子)进行排序?”

最佳答案

您可以获得已按字母顺序排序的所有数据

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

关于swift - UISearchBar 和 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45888891/

相关文章:

c - 如何对近似值进行排序?

c - 按长度排序,在 c 中具有稳定性

swift - 如何使用 swift 为 sqlite 提供数据库路径

ios - WKWebView 两次询问位置权限

ios - FetchedResults Controller 过滤计算值

ios - SwipeCellKit 滑动删除未调用

ios - 如何避免 Reusable Cell 的复制行为

json - 在 Swift 中读取 JSON 输出

ios - 如何在 tableView 之外获取所有自定义 tableview 单元格 textField 和 textView 值

C# array.sort() 按降序排列多个数组