如何显示搜索结果,没有结果就没有结果文字?
How to display search results, and no result text if no result?
我试图只显示结果而不是来自解析服务器的所有数据。我尝试了不同的方法(大部分相同),但我失败了。我知道我错过了什么。
我的问题是如果没有结果,如何在屏幕上只显示结果以及不显示结果文本。
你可以在下面找到我的方法。
非常感谢您。
class SearchTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {
@IBOutlet weak var searchBar: UISearchBar!
var searchActive : Bool = false
var data:[PFObject]!
var noteObjects: NSMutableArray! = NSMutableArray()
var filtered:[PFObject]!
var refresher: UIRefreshControl = UIRefreshControl()
var searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
search()
searchBar.delegate = self
self.refresher.addTarget(self, action: #selector(refresh), for: .valueChanged)
self.tableView?.addSubview(refresher)
self.searchDisplayController?.searchResultsTableView.rowHeight = tableView.rowHeight;
}
@objc func refresh() {
print("Refreshed")
self.refresher.endRefreshing()
search()
}
@objc func search(searchText: String? = nil){
let query = PFQuery(className: "NewUsers")
query.whereKey("user", equalTo: PFUser.current() as Any)
if(searchText != nil){
query.whereKey("name", contains: searchText)
}
query.findObjectsInBackground { (objects, error) in
if error == nil {
if let cars = objects {
for object in cars {
if (object as PFObject?) != nil {
self.data = objects
self.tableView.reloadData()
}
}
}
}
}
}
func updateSearchResults(for searchController: UISearchController) {
self.filtered.removeAll(keepingCapacity: false)
let searchText = searchController.searchBar.text
let query: PFQuery = PFQuery(className: "NewUsers")
if searchController.isActive == true {
query.whereKey("name", matchesRegex: searchText!, modifiers: "i")
self.tableView.reloadData()
}
query.findObjectsInBackground { (results, error) in
self.filtered = results
self.tableView.reloadData()
}
print(searchText as Any)
}
var shouldUseSearchResult : Bool {
if let searchText = searchController.searchBar.text {
if searchText.isEmpty {
return false
}
}
return searchController.isActive
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 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 {
if data != nil {
return self.data.count
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SearchTableViewCell
let obj = self.data[indexPath.row]
self.name = (obj["name"] as? String)!
self.surname = (obj["surname"] as? String)!
self.birthdate = (obj["birthday"] as? String)!
self.age = (obj["age"] as? String)!
self.city = (obj["city"] as? String)!
cell.dateLabel.text = ": " + birthday
cell.ageLabel.text = ": \(age)"
cell.userLabel.text = ": " + name + " " + surname
return cell
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
search(searchText: searchText)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
self.searchBar.resignFirstResponder()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if (self.data.count == 0)
{
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: tableView.bounds.size.height))
noDataLabel.text = "No Result Found."
noDataLabel.textColor = UIColor.red
noDataLabel.textAlignment = .center
self.tableView.backgroundView = noDataLabel
self.tableView.backgroundColor = UIColor.white
self.tablevİEW.separatorStyle = .none
}
return 1
}
self.arrayDealPage
是 array
& self.dealsTable
是 tableView
希望,它可能对您有所帮助,并且添加了 delegate
和 datasource
协议。
我试图只显示结果而不是来自解析服务器的所有数据。我尝试了不同的方法(大部分相同),但我失败了。我知道我错过了什么。
我的问题是如果没有结果,如何在屏幕上只显示结果以及不显示结果文本。
你可以在下面找到我的方法。
非常感谢您。
class SearchTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {
@IBOutlet weak var searchBar: UISearchBar!
var searchActive : Bool = false
var data:[PFObject]!
var noteObjects: NSMutableArray! = NSMutableArray()
var filtered:[PFObject]!
var refresher: UIRefreshControl = UIRefreshControl()
var searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
search()
searchBar.delegate = self
self.refresher.addTarget(self, action: #selector(refresh), for: .valueChanged)
self.tableView?.addSubview(refresher)
self.searchDisplayController?.searchResultsTableView.rowHeight = tableView.rowHeight;
}
@objc func refresh() {
print("Refreshed")
self.refresher.endRefreshing()
search()
}
@objc func search(searchText: String? = nil){
let query = PFQuery(className: "NewUsers")
query.whereKey("user", equalTo: PFUser.current() as Any)
if(searchText != nil){
query.whereKey("name", contains: searchText)
}
query.findObjectsInBackground { (objects, error) in
if error == nil {
if let cars = objects {
for object in cars {
if (object as PFObject?) != nil {
self.data = objects
self.tableView.reloadData()
}
}
}
}
}
}
func updateSearchResults(for searchController: UISearchController) {
self.filtered.removeAll(keepingCapacity: false)
let searchText = searchController.searchBar.text
let query: PFQuery = PFQuery(className: "NewUsers")
if searchController.isActive == true {
query.whereKey("name", matchesRegex: searchText!, modifiers: "i")
self.tableView.reloadData()
}
query.findObjectsInBackground { (results, error) in
self.filtered = results
self.tableView.reloadData()
}
print(searchText as Any)
}
var shouldUseSearchResult : Bool {
if let searchText = searchController.searchBar.text {
if searchText.isEmpty {
return false
}
}
return searchController.isActive
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// 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 {
if data != nil {
return self.data.count
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SearchTableViewCell
let obj = self.data[indexPath.row]
self.name = (obj["name"] as? String)!
self.surname = (obj["surname"] as? String)!
self.birthdate = (obj["birthday"] as? String)!
self.age = (obj["age"] as? String)!
self.city = (obj["city"] as? String)!
cell.dateLabel.text = ": " + birthday
cell.ageLabel.text = ": \(age)"
cell.userLabel.text = ": " + name + " " + surname
return cell
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchActive = false;
self.searchBar.endEditing(true)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
search(searchText: searchText)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
self.searchBar.resignFirstResponder()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if (self.data.count == 0)
{
let noDataLabel: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.tableView.bounds.size.width, height: tableView.bounds.size.height))
noDataLabel.text = "No Result Found."
noDataLabel.textColor = UIColor.red
noDataLabel.textAlignment = .center
self.tableView.backgroundView = noDataLabel
self.tableView.backgroundColor = UIColor.white
self.tablevİEW.separatorStyle = .none
}
return 1
}
self.arrayDealPage
是 array
& self.dealsTable
是 tableView
希望,它可能对您有所帮助,并且添加了 delegate
和 datasource
协议。