ios - 当我在搜索器上搜索内容时得到错误的索引
问题描述
这是一个解释我遇到的问题的例子。
我在一个数组中有 3 个元素。数组的索引匹配每个句子。喜欢
0 索引匹配“你好”(0 索引) 1 索引匹配“nice”(1 索引) 2 索引匹配“你好吗?” (2个索引)
我已将 UISearchBar 放在我的 tableView 中。当我在 searchBar 中键入“h”时,“hello”和“how are you”完美显示,但问题是,当我触摸 tableview 列表上的 2 索引“how are you”时,它显示“nice”在下一个 viewController 上(我有两个 viewController,名为 myIndex 的变量使用下一个 viewController 上的索引),因为该变量获得 1 个索引而不是 2 个索引。我应该如何解决这个问题?
完整代码如下:
import UIKit
class tableviewtest: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var searchBar: UISearchBar!
func tableView(_ tableView: UITableView,
moveRowAt sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath) {
}
var searchArr = [String]()
var searching = false
var copiedArray:[String] = eng // eng is the original array that has the full elements from another view controller.
let cellIdentifier = "xxx"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = self.tableview.dequeueReusableCell(withIdentifier: cellIdentifier, for:indexPath)
if searching {
cell.textLabel?.text = searchArr[indexPath.row]
}
else {
cell.textLabel?.text = copiedArray[indexPath.row]
}
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching {
return searchArr.count
}
else {
return copiedArray.count
}
}
func tableView(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath) {
myIndex = indexPath.row
performSegue(withIdentifier: "segue1", sender: self)
}
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self
self.tableview.register(UITableViewCell.self,forCellReuseIdentifier: cellIdentifier)
}
}
extension tableviewtest: UISearchBarDelegate {
func searchBar (_ searchBar:UISearchBar, textDidChange searchText: String) {
searchArr = copiedArray.filter({$0.prefix(searchText.count) == searchText})
searching = true
tableview.reloadData()
}
}
解决方案
在您从数组中获取字符串的那一刻,您必须考虑searching
状态
let selectedItem = searching ? searchArr[myIndex] : filteredArr[myIndex]
如果搜索字符串为空,则必须重置 searching
func searchBar (_ searchBar:UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
searching = false
searchArr.removeAll()
} else {
searchArr = filteredArr.filter({$0.prefix(searchText.count) == searchText})
searching = true
}
tableview.reloadData()
}
你甚至可以将字符串作为sender
参数传递performSegue
给下一个视图控制器prepare(for segue
func tableView(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = searching ? searchArr[indexPath.row] : filteredArr[indexPath.row]
performSegue(withIdentifier: "segue1", sender: selectedItem)
}
边注:
重新考虑变量的命名:为什么数据源数组一次被调用filteredArr
然后numberOfRows
,为什么非过滤数组仍然被调用 filteredArr
?
推荐阅读
- docker - 如何处理我的 kubernetes 集群中文件上传的并发?
- angular - 如何在 Angular 应用程序中使用 GitHub 进行身份验证登录?
- sql - 查询及物词翻译
- python - 我想通过我的 pqt 主窗口上的按钮打开在 python 中创建的 matplotlib 图
- python - 添加以“h”开头的参数
- python-3.x - 第二次运行 Python 脚本后的 SIGSEGV
- python - Python - 比较两组,如果其中一组更大则返回
- python - 用于将字典键值对公开给 globals() 的 Python 3 和 PEP 样式指南
- javascript - 尝试使用下拉选择更新 DOM
- android - 为什么这会在 toast 消息中打印 0?