swift - 根据 UISearchBar 输入过滤 TableView (Swift)
问题描述
我想要做什么:我想根据 UISearchBar 中的用户输入来过滤上面有文本的 TableViewCells。
我的问题:我让它与简单的细胞一起工作。但是现在我使用了我用结构配置的自定义,现在我不知道如何过滤它们。
这是我的代码:
import UIKit
import Firebase
class searchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet var searchy: UISearchBar!
@IBOutlet var tabley: UITableView!
var searchingNames = [String()]
var searching = false
var datas = [UserSearch]()
override func viewDidLoad() {
super.viewDidLoad()
tabley.delegate = self
tabley.dataSource = self
searchy.delegate = self
populateSearch()
tabley.register(searchTableViewCell.nib(), forCellReuseIdentifier: searchTableViewCell.identifier)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching == true {
return searchingNames.count
} else{
return datas.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if searching == true {
let post = datas[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: searchTableViewCell.identifier, for: indexPath) as! searchTableViewCell
cell.configure(with: post)
return cell
} else{
let post = datas[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: searchTableViewCell.identifier, for: indexPath) as! searchTableViewCell
cell.configure(with: post)
return cell
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchingNames = datas.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()}) // Cannot assign value of type '[UserSearch]' to type '[String]'
// Value of type 'UserSearch' has no member 'lowercased'
searching = true
tabley.reloadData()
}
}
struct UserSearch {
var id: String
var name: String
var pfp: String
}
解决方案
您必须过滤字符串属性,UserSearch
例如name
.
而且您必须考虑用户清除搜索字段以重置searching
为false
并且有一个更有效的 API 来检查子字符串
首先搜索结果的数组必须与数据源数组的类型相同
var searchingNames = [UserSearch]()
替换textDidChange
为
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
searchingNames.removeAll()
searching = false
} else {
searchingNames = datas.filter{ $0.name.range(of: searchText, options: [.anchored, .caseInsensitive]) != nil}
searching = true
}
tabley.reloadData()
}
.anchored
从字符串的开头搜索.caseInsensitive
是不言自明的。
PS:输入cellForRow
是错误的。在searching == true
范围内替换
let post = datas[indexPath.row]
和
let post = searchingNames[indexPath.row]
您甚至可以将代码简化为
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let post = searching ? searchingNames[indexPath.row] : datas[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: searchTableViewCell.identifier, for: indexPath) as! searchTableViewCell
cell.configure(with: post)
return cell
}
推荐阅读
- python-3.x - 使我的遍历 excel 工作表的函数更高效
- java - 我可以在运行时根据变量而不是在 xml 中添加按钮数吗
- java - 调试时,“this”在 Fragment 中不可用
- python - 如何使用熊猫跳过多次加载中的某些行
- python-3.x - 查找不允许我将字符串转换为浮点数的数据
- visual-studio-code - 始终在当前选项卡中从资源管理器打开文件
- machine-learning - 多类分类问题的精确与召回
- ruby-on-rails - Amazon Comprehend Medical 返回过滤值
- api - Varnish 不缓存简单的 GET 请求到后端
- c# - 在 C# 中序列化这些值以在 C++ 中作为已知结构正确读取时遇到问题