gpt4 book ai didi

swift - SearchController - 我不希望在用户尚未开始输入时显示数据

转载 作者:行者123 更新时间:2023-11-28 14:11:30 30 4
gpt4 key购买 nike

]我在我的应用程序中构建了一个 SearchViewController,它只用于搜索用户。

一切都很好,除了当我进入 View Controller 时,它会在我开始搜索之前显示 firebase 数据库中的每个用户?我希望表格 View 在我输入至少 2 个字母之前不显示结果。任何人都知道我可以实现这个吗?

这是我的代码:

class FollowUsersTableViewController: UITableViewController ,UISearchResultsUpdating, UISearchControllerDelegate{

// @IBOutlet var followUsersTableView: UITableView!

func updateSearchResults(for searchController: UISearchController) {
searchController.searchResultsController?.view.isHidden = false
filterContent(searchText: self.searchController.searchBar.text!)
self.tableView.reloadData()
}
private var viewIsHiddenObserver: NSKeyValueObservation?
let searchController = UISearchController(searchResultsController: nil)
var usersArray = [NSDictionary?]()
var filteredUsers = [NSDictionary?]()
var loggedInUser: User?

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

override func viewDidLoad() {
super.viewDidLoad()
//large title
self.title = "Discover"
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
} else {
// Fallback on earlier versions
}
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.searchController.delegate = self;
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar

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

let key = snapshot.key
let snapshot = snapshot.value as? NSDictionary
snapshot?.setValue(key, forKey: "uid")

if(key == self.loggedInUser?.uid) {
print("Same as logged in user, so don't show!")
} else {
self.usersArray.append(snapshot)
//insert the rows
self.tableView.insertRows(at: [IndexPath(row:self.usersArray.count-1,section:0)], with: UITableViewRowAnimation.automatic)
self.tableView.reloadData()
}
}) { (error) in
print(error.localizedDescription)
}

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if searchController.isActive && searchController.searchBar.text != ""{
return filteredUsers.count
}

return self.usersArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FollowTableViewCell

let user : NSDictionary?

if searchController.isActive && searchController.searchBar.text != ""{
user = filteredUsers[indexPath.row]
}
else
{
user = self.usersArray[indexPath.row]
}

cell.title?.text = user?["username"] as? String
let url = URL(string: user?["photoURL"] as! String)!
cell.userImage?.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "user_male"), options: .progressiveDownload, completed: nil)
return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let arr = self.searchController.isActive ? self.filteredUsers : self.usersArray
self.performSegue(withIdentifier: "user", sender: arr[indexPath.row])
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let dest = segue.destination as! UserProfileViewController
let obj = sender as![String:Any]
dest.selectedUser = obj
}

func filterContent(searchText:String)
{
if searchText != ""{
self.filteredUsers = self.usersArray.filter{ user in

let username = user!["username"] as? String

return(username?.lowercased().contains(searchText.lowercased()))!
}
}
}

那么有人对我如何使这项工作有任何提示吗? this is the error im getting

最佳答案

在您的 numberOfRowsInSection 中,您将返回 self.usersArray.count。如果您不显示所有结果,您可以简单地返回 0,并且不会显示任何结果。

如果你想在输入至少 2 个字符时显示搜索,那么使用 searchBar.text 的 count 属性

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

if searchController.isActive && searchController.searchBar.text != "" && searchController.searchBar.text.count >= 2 {
return filteredUsers.count
}

return 0

}

关于swift - SearchController - 我不希望在用户尚未开始输入时显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52485235/

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