在我的例子中,我正在尝试为我的 tableview
数据实现 search
功能。在这里,我将 JSON
数据加载到我的 Tableview section
和 rows
中。在自定义 cell
中,我正在显示名称,我想按名称创建搜索。我尝试了下面的代码,但结果中没有显示任何内容。如何以正确的方式实现搜索功能。
var sections = [Section]()
var filteredNames = [Section]()
var searchController : UISearchController!
表格 View 代表
// MARK: UITableview Delegates
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering {
return filteredNames[section].result.count
} else {
return sections[section].result.count
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section].title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
let item = sections[indexPath.section].result[indexPath.row]
let filteritem = filteredNames[indexPath.section].result[indexPath.row]
if isFiltering {
cell.nameLabel.text = filteritem.name
} else {
cell.nameLabel.text = item.name
}
return cell
}
SearchBarAction 和代理
// MARK: UISearchBar Delegates
@IBAction func searchAction(_ sender: Any) {
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.keyboardType = UIKeyboardType.asciiCapable
searchController.searchBar.barTintColor = #colorLiteral(red: 0.317096545, green: 0.5791940689, blue: 0.3803742655, alpha: 1)
searchController.searchBar.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
searchController.dimsBackgroundDuringPresentation = false
// Make this class the delegate and present the search
self.searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
present(searchController, animated: true, completion: nil)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
guard let searchText = searchBar.text else {
isFiltering = false
return
}
filteredNames = sections[indexPath.section].result[indexPath.row].filter({
return $0.lowercased().contains(searchText.lowercased())
})
isFiltering = filteredNames.count > 0
self.tableView.reloadData()
}
Error: I am getting error - Use of unresolved identifier 'indexPath'; did you mean 'IndexPath'?
最佳答案
首先你要过滤部分 所以命名数组
var filteredSections = [Section]()
filteredNames 具有误导性,暗示数组是 [String]
一个可能的解决方案是使用过滤后的内容创建新的部分
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if !searchText.isEmpty {
filteredSections = sections.compactMap { section -> Section? in
let filteredContent = section.result.filter {$0.name.range(of: searchText, options: .caseInsensitive) != nil }
return filteredContent.isEmpty ? nil : Section(title: section.title, result: filteredContent)
}
isFiltering = true
} else {
filteredSections.removeAll()
isFiltering = false
}
self.tableView.reloadData()
}
并且表格 View 数据源方法必须更改为
func numberOfSections(in tableView: UITableView) -> Int {
return isFiltering ? filteredSections.count : sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let currentSection = isFiltering ? filteredSections[section] : sections[section]
return currentSection.result.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return isFiltering ? filteredSections[section].title : sections[section].title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier, for: indexPath) as! MyCustomCell
let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section]
let item = section.result[indexPath.row]
cell.nameLabel.text = item.name
return cell
}
关于ios - 如何使用 Swift 创建 UISearchBar 以搜索我的 Tableview 数据的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57508237/