Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Ios Swift中的搜索栏不使用PHP文件搜索_Ios_Swift - Fatal编程技术网

Ios Swift中的搜索栏不使用PHP文件搜索

Ios Swift中的搜索栏不使用PHP文件搜索,ios,swift,Ios,Swift,我的代码中没有错误,但肯定缺少一些东西。还有,当我在键盘上按“完成”时,什么也没有发生。这可能与搜索栏函数或表视图函数有关。我还有一个Search.swift文件,我将添加它。任何建议都会很有帮助,我觉得自己被卡住了 SearchBarViewController: let url = URL(string: "http://127.0.0.1/musicfiles/search.php") var filteredData = [String]() var shouldSh

我的代码中没有错误,但肯定缺少一些东西。还有,当我在键盘上按“完成”时,什么也没有发生。这可能与搜索栏函数或表视图函数有关。我还有一个Search.swift文件,我将添加它。任何建议都会很有帮助,我觉得自己被卡住了

SearchBarViewController

let url = URL(string: "http://127.0.0.1/musicfiles/search.php")
var filteredData = [String]()
var shouldShowSearchResults = false
var search: [Search] = []
var filePath = "http://127.0.0.1/musicfiles/search.php"

override func viewDidLoad() {
    super.viewDidLoad()
    
    tableView.delegate = self
    tableView.dataSource = self
    searchBar.delegate = self
    searchBar.returnKeyType = UIReturnKeyType.done
    
    let task = URLSession.shared.dataTask(with: url!) { (data, snapshot, error) in
        let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
        print(retrievedList!)
        self.parseSongs(data: retrievedList!)
    }
    task.resume()
}

func parseSongs (data: String) {
    if (data.contains("*")) {
        let dataArray = (data as String).split(separator: "*").map(String.init)
        for item in dataArray {
            let itemData = item.split(separator: ",").map(String.init)
            let searchSong = Search(songname: itemData[0])
            search.append(searchSong!)
            
            for s in search {
                print(s.searchSongName())
            }
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return search.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DataCell
    let song = search[indexPath.row].searchSongName()
    cell.congigureCell(text: song)
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let searchSong = search[indexPath.row].searchSongName()
    let fileURLString = "\(filePath)\(searchSong)"
    print(fileURLString)
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == "" {
        
        shouldShowSearchResults = false
        
        view.endEditing(true)
        
        filteredData.removeAll()
    } else {
        shouldShowSearchResults = true
        filteredData = search.filter({ (songName) -> Bool in
            songName.searchSongName().range(of: searchText) != nil
        })
            .map { $0.songname }
    }
    tableView.reloadData()
}

您正在将数据加载到名为
search
的数组中

过滤数据时,将过滤后的数据放入名为
filteredData
的数组中

tableview始终显示
搜索的内容,因此您永远看不到筛选结果

您可以检查
filteredData
是否为空,然后从该数组返回数据,或者在
numberOfRows
cellForRow
中搜索
。就我个人而言,我总是使用
filteredData
,并确保当过滤器字符串为空时,它保存
search
的内容

var filteredData = [Search]()
func parseSongs (data: String) {
    if (data.contains("*")) {
        let dataArray = (data as String).split(separator: "*").map(String.init)
        for item in dataArray {
            let itemData = item.split(separator: ",").map(String.init)
            let searchSong = Search(songname: itemData[0])
            search.append(searchSong!)

            for s in search {
                print(s.searchSongName())
            }
            self.filterData = self.search
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredData.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DataCell
    let song = filteredData[indexPath.row].searchSongName()
    cell.congigureCell(text: song)
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let searchSong = filteredData[indexPath.row].searchSongName()
    let fileURLString = "\(filePath)\(searchSong)"
    print(fileURLString)
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if (searchBar.text ?? "").isEmpty {
        view.endEditing(true)

        filteredData = search
    } else {
        filteredData = search.filter({ (songName) -> Bool in
            songName.searchSongName().range(of: searchText) != nil
        })
    }
    tableView.reloadData()
}