我有一个连接到Tableview的搜索栏,但我不知道如何使其工作!表格视图是“存储器”的表格视图。记忆中包含标题,用户ID,正文...,我希望它也能够按标题搜索。事实是,我正在使用Firebase,但我真的不知道如何在特定用户的特定记忆中进行搜索以及如何从Firebase中获取所有信息。
我尝试了这段代码,但是似乎没有用。现在的问题是,它甚至没有将记忆带给用户在tableView中。有什么想法吗?
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
class MemoryViewController: UIViewController,UITableViewDelegate,UITableViewDataSource, UISearchBarDelegate, UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
if searchController.searchBar.text!.isEmpty {
self.filteredArray = self.memories
} else {
self.filteredArray = self.memories.filter({
$0.title.localizedCaseInsensitiveContains(searchController.searchBar.text!)
})
}
self.tbl.reloadData()
}
var filteredArray : [Memory] = []
@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 = ""
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredArray.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 = filteredArray[indexPath.row]
cell?.textLabel?.text = temp.title
cell?.imageView?.image = temp.image
return cell!
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete
{
let temp = self.filteredArray[indexPath.row]
self.filteredArray.remove(at: indexPath.row)
if let index = self.memories.firstIndex(where: { $0.title == temp.title }) {
self.memories.remove(at: index)
}
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.tbl.delegate = self
self.tbl.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()
}
}
})
}
self.filteredArray = self.memories
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)!]
}
}
}
}