ios - 如何使搜索结果不显示在搜索栏中的表格 View 中?

标签 ios swift

我的目标是让我的工作 php 搜索文件中的搜索结果显示在我的表格 View 中。我的搜索 View Controller 中没有显示任何错误,但肯定有一些东西丢失了,我无法弄清楚。我还有另一个 .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()
       }
}
}

最佳答案

首先你要过滤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 中的可选绑定(bind) (cellForRow)是没有意义的。如果存在设计错误,代码将崩溃,问题可以立即修复。

关于ios - 如何使搜索结果不显示在搜索栏中的表格 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61583810/

相关文章:

swift 2 : Throw from closure

ios - 当您按下按钮时,在 Swift 中的 UicollectionViewController 中添加单元格

ios - FBSDKCoreKit错误: "Include of non-modular header inside framework module"

iOS。如果来自不同的目标,则获取 bundle

ios - swift 中调用初始化程序错误消息时没有完全匹配

ios - 如何使用钥匙串(keychain)验证用户名和密码

iOS 8 map 应用副本

ios - Swift 将箭头和 sourceView 添加到 UIAlertController

ios - 追加时从 NSArray 中删除重复值

xcode - 属性 'public' 只能在非本地范围内使用