gpt4 book ai didi

Swift - 在 UITableView 中搜索

转载 作者:行者123 更新时间:2023-11-30 10:41:23 25 4
gpt4 key购买 nike

我试图在我的应用中使用 UISearchBar 搜索我的 Firebase 数据,但我陷入困境。我在 TableView 中成功接收来自 Firebase 的数据。我有一个内存写入应用程序,用户可以创建内存(显示在 firebase 的表格 View 中)。记忆有标题、描述、图片和日期,我希望它能够通过标题搜索记忆。

我这里有一段代码由于某种原因不起作用...如果您能帮助我找出代码中的问题或找到此代码的替代品,我会很高兴:)

MemoryTitles 类:

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

内存 View Controller :

class MemoryViewController: UIViewController,UITableViewDelegate,UITableViewDataSource


// the filtered memories array for the search bar
var memoriesTitlesArr: [String] = []
var filteredDataa: [String] = []

// connections from storyboard to the code

@IBOutlet weak var tbl: UITableView!

@IBOutlet weak var searchBar: UISearchBar!

// an array of memories
var memories : [Memory] = []

var ref = Database.database().reference()
let sref = Storage.storage().reference()

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


// TableView functions

// Return the number of rows in section
// section - an index number identifying a section in tableView.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching {
return filteredDataa.count
} else {
return memories.count
}

// return memories.count
}

// Return Cell for row function : an object inheriting from UITableViewCell
// indexPath - an index path locating a row in tableView.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "iden")
if searching {
cell?.textLabel?.text = filteredDataa[indexPath.row]
} else {
var cell : UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: "iden", for: indexPath)

if cell == nil
{
cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "iden")
}
let temp = memories[indexPath.row]
cell?.textLabel?.text = temp.title
cell?.imageView?.image = temp.image

return cell!
}
return cell!
}

// Can edit row : asks the data source to verify that the given row is editable.
// indexPath - an index path locating a row in tableView.

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

return true // true if the row indicated by indexPath is editable; otherwise, false.
}

// Asks the data source to commit the insertion or deletion of a specified row.
// indexPath - an index path locating a row in tableView.

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

if editingStyle == .delete // editingStyle - the cell editing style corresponding to a insertion or deletion requested for the row specified by indexPath.
{
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.tbl.delegate = self
self.tbl.dataSource = self
}

// click on bar-Button function
@objc func barButtonItemClicked(_ sender:UIBarButtonItem)
{
print("+ clicked") // writes "+ clicked"
let addMemoryViewController = self.storyboard?.instantiateViewController(withIdentifier: "AddMemoryViewController") as! AddMemoryViewController

self.strMode = "newMemory"
self.navigationController?.pushViewController(addMemoryViewController, animated: true)
}
// Reading from NSUserDefault (A class that provides simple storage of different data types solution.)
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 date = d.object(forKey: "date") as? NSNumber
let m = Memory(title: strTitle!, body: strBody!, key: key!, uid: uid!, imageRef: strImageRef!, date: date!) // A variable from type memory
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 = " "
}

// loading the memories from the Database
func loadMemories()

{
let UID = Auth.auth().currentUser!.uid
self.ref.child("MEmories").queryOrdered(byChild: "uid").queryEqual(toValue: UID).observeSingleEvent(of: .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 date = d.value["date"] as? NSNumber


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

m.key = d.key

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


tempImageRef.getData(maxSize: 500*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.memoriesTitlesArr.append(title!)
}
}//end of if
})
}

// Notifies the view controller that a segue is about to be performed.
// segue - The segue object containing information about the view controllers involved in the segue.
// senderThe object that initiated the segue.
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)!]

}
}
}
var searching = false
}

extension MemoryViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredDataa = memoriesTitlesArr.filter({ $0.prefix(searchText.count)==searchText.lowercased()})
searching = true
tbl.reloadData()

}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
searchBar.text = ""
tbl.reloadData()
}
}

最佳答案

以下是如何让它发挥作用。

1.首先,不需要searching维持搜索和不搜索的状态。

2. 其次,使用filteredData作为dataSource对于 tableView而不是memoriesfilteredData最初将包含 memories 中的所有对象,即

var memories : [Memory] = []
lazy var filteredData = self.memories

UITableViewDataSource方法就像,

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "iden") {
cell.textLabel?.text = self.filteredData[indexPath.row].title
return cell
}
return UITableViewCell()
}

现在,在搜索时更新 filteredData经过过滤memories使用相关条件,即

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.filteredData = self.memories.filter({ $0.title.hasPrefix(searchText) })
//change the condition as per your requirement......
self.tbl.reloadData()
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.text = nil
self.filteredData = self.memories
self.tbl.reloadData()
}

取消后,重新填写filteredData与整体memories数据。

关于Swift - 在 UITableView 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56722491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com