gpt4 book ai didi

swift - 如何显示搜索结果,没有结果就没有结果文字?

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

我试图只显示结果而不是解析服务器的所有数据。我尝试了不同的方法(大部分相同),但我失败了。我知道我错过了什么。

我的问题是如果没有结果,如何在屏幕上只显示结果以及不显示结果文本。

您可以在下面找到我的方法。

非常感谢您。

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.arrayDealPagearray & self.dealsTabletableView希望,它可能对您和 delegate 有所帮助和 datasource添加协议(protocol)。

关于swift - 如何显示搜索结果,没有结果就没有结果文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46970992/

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