ios - 使用 UIExfield 搜索
问题描述
我想问一下。我如何搜索从对象映射的字符串数组而不是从 uitextfield 搜索它,我尝试了这种方法,但它没有用。我哪里做错了?这是我的代码设置。
var cities = [City]()
var cityNames = [String]()
private func populateCities() {
BasicInfoServices.shared.getCity { [weak self] result in
switch result {
case .success(let cities):
self?.cities = cities
let names = cities.compactMap { $0.name }
self?.cityNames = names
case .failure(let error):
print(error)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cityNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CitiesAlertCell.cellId, for: indexPath) as! CitiesAlertCell
cell.selectionStyle = .none
let item = cityNames[indexPath.row]
cell.item = item
return cell
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
searchTextField.resignFirstResponder()
cityNames.removeAll()
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if searchTextField.text?.count != nil {
self.cityNames.removeAll()
for str in cityNames {
let range = str.lowercased().range(of: textField.text!, options: .caseInsensitive, range: nil, locale: nil)
if range != nil {
cityNames.append(str)
}
}
}
tableView.reloadData()
return true
}
解决方案
所以,去除杂乱,你基本上是在做这个......
self.cityNames.removeAll()
for str in cityNames {
//...
cityNames.append(str)
}
您从数组中删除所有元素并尝试对其进行迭代,但是由于现在数组中没有任何内容,因此没有可以迭代的内容。
您可以使用“过滤”数组,它从“主”数组中获取输入。“过滤”数组是向用户显示的内容
var filteredCities: [String]()
private func populateCities() {
BasicInfoServices.shared.getCity { [weak self] result in
switch result {
case .success(let cities):
self?.cities = cities
let names = cities.compactMap { $0.name }
self?.cityNames = names
self?.filteredCities = names
case .failure(let error):
print(error)
}
}
}
然后,当您需要过滤列表时...
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if searchTextField.text?.count != nil {
self.filteredCities.removeAll()
for str in cityNames {
let range = str.lowercased().range(of: textField.text!, options: .caseInsensitive, range: nil, locale: nil)
if range != nil {
filteredCities.append(str)
}
}
}
tableView.reloadData()
return true
}
这当然意味着您将需要使用filteredCities
而不是cityNames
在您的表数据源中;)
说了这么多。我会考虑做一些稍微不同的事情。而不是使用主/过滤器列表,只需将cities
数组用作主并直接过滤它......
if let text = searchTextField.text, !text.isEmpty else {
cityNames = cities.filter { $0.name.lowercased().range(of: text, options: .caseInsensitive, range: nil, locale: nil) != nil }.map { $0.name }
} else {
cityNames = cities.map { $0.name }
}
如果速度是一个问题,您可以回退到主/过滤String
值列表,这将允许您消除对map
结果的需要,但在这一点上,我会质疑为什么您仍然需要一个String
列表
例如...
var cities = [City]() // Master
var cityFilter = [City]() // Filter
private func populateCities() {
BasicInfoServices.shared.getCity { [weak self] result in
switch result {
case .success(let cities):
self?.cities = cities
self?.cityFilter = cities
case .failure(let error):
print(error)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cityFilter.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CitiesAlertCell.cellId, for: indexPath) as! CitiesAlertCell
cell.selectionStyle = .none
let item = cityFilter[indexPath.row]
cell.item = item // The item is now a city reference
return cell
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
searchTextField.resignFirstResponder()
cityFilter = cities
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let text = searchTextField.text, !text.isEmpty else {
cityFilter = cities.filter { $0.name.lowercased().range(of: text, options: .caseInsensitive, range: nil, locale: nil) != nil }
} else {
cityFilter = cities
}
tableView.reloadData()
return true
}
如果您担心城市nil
名称,那么我会filter
使用原始列表,以便主列表仅包含有效值
推荐阅读
- html - 使用 ng-repeat 仅用于解析但不打印大量 html 元素
- php - 无法从 php 中的 xml 加载数据
- java - 无法执行 powershell 命令在 Windows Server 2016 中创建集群
- javascript - 脱咖啡因:双括号
- java - 如何一次调用两个参与者,即并行?
- c# - 如何检查是否在 Windows 设备上启用了自动日期时间设置?
- css - Angular mat-table:是否可以按列合并单元格?
- python - 使用 Django 转到另一个页面时,如何在 URL 中保留一部分查询字符串?
- go - Go Tour 练习:Web Crawler - 所有 goroutine 都处于休眠状态 - 死锁
- oracle - 我可以在光标中使用 BFILENAME 来处理多个图像吗?