使用Firebase连接到Tableview的搜索栏

时间:2019-05-22 12:00:58

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

我有一个连接到Tableview的搜索栏,但我不知道如何使其工作! 表格视图是“存储器”的表格视图。记忆中包含标题,用户ID,正文,我希望它也能够按标题搜索。 事实是,我正在使用Firebase,但我真的不知道如何在特定用户的特定记忆中进行搜索以及如何从Firebase中获取所有信息。

我尝试了很多代码行,但没有任何效果。我需要帮助!

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase

class MemoryViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate, UISearchResultsUpdating {

    func updateSearchResults(for searchController: UISearchController) {
        self.memories = Array.filter({ (array: String) -> Bool in

            if array.contains(searchController.searchBar.text!)
            {
                return true
            }
            else
            {
                return false
            }

        })

        self.resultController.tableView.reloadData()

    }
    var filteredArray = [Memory]()

    var searchController = UISearchController()
    var resultController = UITableViewController()

    @IBOutlet weak var tbl: UITableView!

    @IBOutlet weak var searchBar: UISearchBar!

    var memories : [Memory] = []

    var ref : DatabaseReference!
    let sref = Storage.storage().reference()

    var lastIndex : Int = 0
    var strMode : String = ""

    var filteredData = [String]()



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

        return memories.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "iden"

        var cell : UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)

        if cell == nil
        {
            cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: identifier)
        }

        let temp = memories[indexPath.row]
        cell?.textLabel?.text = temp.title
        cell?.imageView?.image = temp.image
        return cell!
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

        return true

    }


    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete
        {
            let temp = self.memories[indexPath.row]

            self.memories.remove(at: indexPath.row)

            self.ref.child("MEmories/\(temp.key)").removeValue()

            tableView.deleteRows(at: [indexPath as IndexPath], with: .fade)
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        ref = Database.database().reference()
        let rightButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(MemoryViewController.barButtonItemClicked(_:)))

        self.navigationItem.setRightBarButton(rightButton, animated: true)
        // Do any additional setup after loading the view.

        self.loadMemories()

        self.searchController = UISearchController(searchResultsController: resultController)
        tbl.tableHeaderView = self.searchController.searchBar

        self.searchController.searchResultsUpdater = self

        self.resultController.tableView.delegate = self
        self.resultController.tableView.dataSource = self



    }

    @objc func barButtonItemClicked(_ sender:UIBarButtonItem)
    {
        print("+ clicked")
        let addMemoryViewController = self.storyboard?.instantiateViewController(withIdentifier: "AddMemoryViewController") as! AddMemoryViewController

        self.strMode = "newMemory"


        self.navigationController?.pushViewController(addMemoryViewController, animated: true)



    }


    func readFromNSUSerDefault()-> Memory

    {

        let d : UserDefaults = UserDefaults.standard

        let strTitle = d.object(forKey: "title") as? String

        let strBody = d.object(forKey: "body") as? String

        let strImageRef = d.object(forKey: "imageRef") as? String

        let uid = d.object(forKey: "uid") as? String

        let imageData = d.object(forKey: "imageData") as? Data

        let key = d.object(forKey: "key") as? String



        let m = Memory(title: strTitle!, body: strBody!, key: key!, uid: uid!, imageRef: strImageRef!)

        m.image = UIImage(data: imageData!)

        m.key = key!

        return m

    }

    override func viewDidAppear(_ animated: Bool) {

        let d = UserDefaults.standard
        let newMemory = readFromNSUSerDefault()
        let userAdded = d.bool(forKey: "userAdded") //key new user = true
        let userEdited = d.bool(forKey: "userEdited")//key  user edited = true

        if self.strMode == "newMemory" &&  userAdded

        {
            self.memories.append(newMemory)
            self.tbl.reloadData()
        }

        else if self.strMode == "edit" && userEdited
        {
            memories[lastIndex] = newMemory
            self.tbl.reloadData()
        }


        d.set(false, forKey: "userAdded")
        d.set(false, forKey: "userEdited")

        d.synchronize()

        self.strMode = " "


    }

    func loadMemories()

    {

        self.ref.child("MEmories").queryOrderedByKey().observe(.value, with: {

            snapShot in

            if let dict = snapShot.value as? NSDictionary

            {

                for d in (dict as? Dictionary<String,AnyObject>)!

                {

                    let title = d.value["title"] as?String

                    let body = d.value["body"] as? String

                    let uid = d.value["uid"] as? String

                    let imageRef = d.value["imageRef"] as? String



                    let m = Memory(title: title!, body: body!, uid: uid!,imageRef:imageRef!)

                    m.key = d.key

                    let tempImageRef = self.sref.child(m.imageRef)



                    tempImageRef.getData(maxSize: 1*1024*1024, completion: {(data,error) in



                        if error == nil

                        {

                            if let imageData = data

                            {

                                m.image = UIImage(data: imageData)

                                self.memories.append(m)

                                self.tbl.reloadData()

                            }

                        }

                    })

                }



            }//end of if

            self.ref.child("MEmories").removeAllObservers()



        })

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if let identifier = segue.identifier

        {

            if identifier == "goToEdit"

            {

                let indexPath = self.tbl.indexPathForSelectedRow

                let addMemoryViewController = segue.destination as! AddMemoryViewController

                self.strMode = "edit"

                self.lastIndex = (indexPath?.row)!

                addMemoryViewController.mode = self.strMode

                addMemoryViewController.current = memories[(indexPath?.row)!]

            }

        }

    }

}

1 个答案:

答案 0 :(得分:0)

根据评论,该问题与Firebase并不直接相关,但原始发帖人正在询问此问题

  

我想按内存标题过滤代码,但我仍然   卡住...有什么想法吗? – ABM

因此,这是一个完整示例,说明如何按内存标题过滤代码。

让我们从一个简单的类开始,该类用于保存内存及其标题

class MemoryClass {
    var title = ""
    init(withTitle: String) {
        self.title = withTitle
    }
}

然后,我们将构建一个数组,该数组将用作例如tableView dataSource

let m0 = MemoryClass(withTitle: "Gone Fishing")
let m1 = MemoryClass(withTitle: "Wedding")
let m2 = MemoryClass(withTitle: "France Vacation")
let m3 = MemoryClass(withTitle: "Italy Vacation")

let memoriesArray = [m0, m1, m2, m3]

,然后是按标题查找内存的代码。在这种情况下,我们要查找标题为“ Wedding”的内存

let memoryTitleToFind = "Wedding"
let filteredArray = memoriesArray.filter { $0.title == memoryTitleToFind }

并验证是否找到了内存,让我们遍历找到的内存。请记住,如果有几个标题为“ Wedding”的内存,那么它将把它们全部过滤到过滤后的数组中

for foundMemory in filteredArray {
    print(foundMemory.title)
}

并且显示过滤结果的输出是

Wedding

我为此答案创建了一个简单的MemoryClass,在您的问题中,您还注意到其他属性

  

内存具有标题,用户ID,正文,我也希望它也能够搜索   按标题

因此可以将它们添加到类中并以相同的方式进行过滤。