搜索栏为空时重新加载 table 视图
reloading table view when search bar is empty
我是 swift.I 的新手,已经实施了 json 的搜索操作,显示在 table 视图中关于描述字段。
func updateSearchResults(for searchController: UISearchController) {
// If we haven't typed anything into the search bar then do not filter the results
if searchController.searchBar.text! == "" {
filteredsneakernews = [classObject.descriptionn]
self.results = self.placeHolderArray
self.DiscoveryNewsTableView.reloadData()
} else {
// Filter the results
//filteredsneakernews = [classObject.descriptionn].filter { [=11=].description.lowercased().contains(searchController.searchBar.text!.lowercased()) }
results = results.filter {
[=11=].description.lowercased().contains(searchController.searchBar.text!.lowercased()) }
// self.results = self.placeHolderArray
self.DiscoveryNewsTableView.reloadData()
}
// self.DiscoveryNewsTableView.reloadData()
}
这是 table 视图的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
classObject.getDataForTableView(results: results, index: indexPath.row)
//let image_url = filteredsneakernews[indexPath.row].image
// cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
self.placeHolderArray = self.results
cell.newsTitle.text = classObject.descriptionn
let imageURLPathString = newsurl + classObject.image
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
cell.sneakerImageView.image = image
}
return cell
}
这是我为从 json
获取数据而制作的 class
class getData: NSObject {
var descriptionn : String = ""
var image : String = ""
// static let shared = getData()
func getDataForTableView(results: [[String:String]], index : Int){
var productArray = [String:String]()
productArray = results[index]
descriptionn = productArray["description"]!
image = productArray["images"]!
}
}
搜索结果的搜索栏。当我完成搜索时 当搜索栏为空时,我希望 table 视图应该重新加载数据。当搜索栏为空时,它应该在 table 视图中显示所有记录。但它是搜索栏为空时不重新加载 table 视图 。您可以从此 link 下载代码。 https://drive.google.com/file/d/1G3QUYLgLDwIjNKb-HB-zO98l14HrcRVe/view?usp=sharing
@ajadka 我看过你的代码,有一个小错误......只需替换这一行 n 它就会正常工作......
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
// self.placeHolderArray = self.results comment this line here
...... }
并将该行放在这里
func single_news(userid: Int) {
.......
self.results = jsonValue.object(forKey: "data") as! [[String:String]]
self.placeHolderArray = self.results //put here that line
..........
}
我是 swift.I 的新手,已经实施了 json 的搜索操作,显示在 table 视图中关于描述字段。
func updateSearchResults(for searchController: UISearchController) {
// If we haven't typed anything into the search bar then do not filter the results
if searchController.searchBar.text! == "" {
filteredsneakernews = [classObject.descriptionn]
self.results = self.placeHolderArray
self.DiscoveryNewsTableView.reloadData()
} else {
// Filter the results
//filteredsneakernews = [classObject.descriptionn].filter { [=11=].description.lowercased().contains(searchController.searchBar.text!.lowercased()) }
results = results.filter {
[=11=].description.lowercased().contains(searchController.searchBar.text!.lowercased()) }
// self.results = self.placeHolderArray
self.DiscoveryNewsTableView.reloadData()
}
// self.DiscoveryNewsTableView.reloadData()
}
这是 table 视图的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
classObject.getDataForTableView(results: results, index: indexPath.row)
//let image_url = filteredsneakernews[indexPath.row].image
// cell.sneakerImageView.image=filteredsneakernews[indexPath.row].image
self.placeHolderArray = self.results
cell.newsTitle.text = classObject.descriptionn
let imageURLPathString = newsurl + classObject.image
let url1 = URL(string: imageURLPathString)
print("xyz", url1)
let data = try? Data(contentsOf: url1!)
if let imageData = data {
let image = UIImage(data: imageData)
cell.sneakerImageView.image = image
}
return cell
}
这是我为从 json
获取数据而制作的 classclass getData: NSObject {
var descriptionn : String = ""
var image : String = ""
// static let shared = getData()
func getDataForTableView(results: [[String:String]], index : Int){
var productArray = [String:String]()
productArray = results[index]
descriptionn = productArray["description"]!
image = productArray["images"]!
}
}
搜索结果的搜索栏。当我完成搜索时 当搜索栏为空时,我希望 table 视图应该重新加载数据。当搜索栏为空时,它应该在 table 视图中显示所有记录。但它是搜索栏为空时不重新加载 table 视图 。您可以从此 link 下载代码。 https://drive.google.com/file/d/1G3QUYLgLDwIjNKb-HB-zO98l14HrcRVe/view?usp=sharing
@ajadka 我看过你的代码,有一个小错误......只需替换这一行 n 它就会正常工作......
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "discoveryNewscell") as! DiscoveryNewsTableViewCell
// self.placeHolderArray = self.results comment this line here
...... }
并将该行放在这里
func single_news(userid: Int) {
.......
self.results = jsonValue.object(forKey: "data") as! [[String:String]]
self.placeHolderArray = self.results //put here that line
..........
}