Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 搜索栏不会过滤数据_Json_Swift_Uisearchbar - Fatal编程技术网

Json 搜索栏不会过滤数据

Json 搜索栏不会过滤数据,json,swift,uisearchbar,Json,Swift,Uisearchbar,我正在尝试使用搜索栏过滤获取的JSON数据。但是,当我在搜索栏中键入内容时,它什么也不做。数据仍然在同一个位置,并且没有被过滤,但是当我在搜索栏中键入内容时,应该对其进行动态过滤 下面的代码显示了我的TableViewController以及将JSON数据提取到数组中的函数。然后使用搜索栏对其进行过滤,每当数据的名称与搜索栏中的条件匹配时,就会将其添加到第二个名为“filteredExercise”的数组中 import UIKit class ExerciseTableViewControl

我正在尝试使用搜索栏过滤获取的JSON数据。但是,当我在搜索栏中键入内容时,它什么也不做。数据仍然在同一个位置,并且没有被过滤,但是当我在搜索栏中键入内容时,应该对其进行动态过滤

下面的代码显示了我的TableViewController以及将JSON数据提取到数组中的函数。然后使用搜索栏对其进行过滤,每当数据的名称与搜索栏中的条件匹配时,就会将其添加到第二个名为“filteredExercise”的数组中

import UIKit

class ExerciseTableViewController: UITableViewController, UISearchBarDelegate {

var fetchedExercise = [Exercise]()
var filteredExercise = [Exercise]()
var inSearchMode = false

@IBOutlet var searchBar: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    searchBar.delegate = self

    parseData()

}


func parseData() {

    fetchedExercise.removeAll()

    let urlPath = "https://wger.de/api/v2/exercise/?format=json&language=2&status=2&limit=200"
    let url = URL(string: urlPath)!

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in

        if error != nil {
            print("Error while parsing JSON")
        }
        else {

            do {
                if let data = data,
                    let fetchedData = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String:Any],
                    let exercises = fetchedData["results"] as? [[String: Any]] {


                    for eachExercise in exercises {
                        if eachExercise["license_author"] as! String == "wger.de" {
                            let name = eachExercise["name"] as! String
                            let description = eachExercise["description"] as! String
                            let id = eachExercise["id"] as! Int

                            self.fetchedExercise.append(Exercise(name: name, description: description, id: id))
                        }
                    }


                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                }
            }
            catch {
                print("Error while parsing data.")
            }
        }
    }
    task.resume()
}



// 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 inSearchMode {

        return filteredExercise.count
    }

    return fetchedExercise.count
}


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

        let exercise: Exercise!

        if inSearchMode {

            exercise = filteredExercise[indexPath.row]
            cell.configureCell(exercise: exercise)

        } else {

            exercise = fetchedExercise[indexPath.row]
            cell.configureCell(exercise: exercise)
        }

        return cell

    } else {

        return UITableViewCell()
    }


}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    var exercise: Exercise!

    exercise = fetchedExercise[indexPath.row]

    performSegue(withIdentifier: "exerciseDetailVC", sender: exercise)
}


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if searchBar.text == nil || searchBar.text == "" {

        inSearchMode = false
        self.tableView.reloadData()

    } else {

        inSearchMode = true

        let lower = searchBar.text!.lowercased()

        filteredExercise = fetchedExercise.filter({$0.name.range(of: lower) != nil})
        self.tableView.reloadData()
    }
}



}

看起来你在这方面有错误

@IBOutlet var searchBar: UITableView!

我认为它应该是UISearchBarController类型。

好的,我终于找到了它的问题所在

基本上首先,由于Xcode错误,我的搜索栏的类型不正确,我没有看到这一点。 然后我不得不将我的IBOutlet连接到故事板,因为它还没有完成。
最后,在筛选数据时,我开始得到错误的结果,这是因为我一直在使用小写()函数筛选结果,而我的所有数据都是大写的。

哦,这是真的。我已将其更改为“@ibvar searchBar:UISearchBar!”但它仍然不能解决问题。如果你没有,你可能需要重新连接插座,而不是仅仅更改文本。一旦更改IBOutlet连接文本,它将断开连接。看起来您的代理也设置好了,但如果没有设置,也可能是个问题。你也可以通过故事板把它(代表)连接起来。如果答案对你有帮助,请作为接受者检查答案