我是swift的新手。我已经在表格视图中为json show实现了关于描述字段的搜索操作。
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 { $0.description.lowercased().contains(searchController.searchBar.text!.lowercased()) }
results = results.filter {
$0.description.lowercased().contains(searchController.searchBar.text!.lowercased()) }
// self.results = self.placeHolderArray
self.DiscoveryNewsTableView.reloadData()
}
// self.DiscoveryNewsTableView.reloadData()
}
以下是表格视图的代码
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 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"]!
}
}
搜索结果的搜索栏。当我完成搜索当搜索栏为空时我希望表视图应该重新加载数据。当搜索栏为空时,应该在表视图中显示所有记录。但是当搜索栏为空时它不会重新加载表视图< / strong>。您可以从此链接下载代码。 https://drive.google.com/file/d/1G3QUYLgLDwIjNKb-HB-zO98l14HrcRVe/view?usp=sharing
答案 0 :(得分:0)
@ajadka我已经看到了你的代码并且存在一些小错误...只需更换此行即可正常工作..
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
..........
}