如何使搜索结果不显示在搜索栏中的表格视图中?

时间:2020-05-04 01:23:27

标签: ios swift

我的目标是使工作的php搜索文件中的搜索结果显示在表格视图中。搜索视图控制器中没有显示任何错误,但是肯定缺少某些内容,我无法弄清楚。我也有另一个.swift文件。我不确定问题出在哪里。有什么建议吗?

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

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 numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

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

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 == "" {
           
           isSearching = false
           
           view.endEditing(true)
           
           tableView.reloadData()
       } else {
           isSearching = true
           
           tableView.reloadData()
       }

} }

1 个答案:

答案 0 :(得分:2)

首先,您必须过滤textDidChange中的数据

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
   if searchText.isEmpty {
       isSearching = false
       view.endEditing(true)
       filteredData.removeAll()      
   } else {
       isSearching = true
       filteredData = search.filter{ $0.songname.range(of: searchText, options: .caseInsensitive) != nil }
   }
   tableView.reloadData()
}

然后,您必须更改所有数据源方法以显示searchfilteredData数组。

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

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

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

    let searchSong =  isSearching ? search[indexPath.row] : filteredData[indexPath.row]
    let fileURLString = "\(filePath)\(searchSong.songname)"
    print(fileURLString)

}

注意:

if let cell中的可选绑定(cellForRow)是没有意义的。如果存在设计错误,则代码将崩溃,并且可以立即解决此问题。