arrays - 搜索 UITableView 的多个部分,数组结构数组
问题描述
这里是业余的。我真的很希望能够通过我创建的联系人 tableview 控制器进行搜索。过去几天我一直在尝试各种不同的想法,但没有任何问题。似乎我能找到的只是如何搜索一个数组,但我的部分似乎把它全部扔掉了,我只是不够聪明,不知道要修复的代码。我有 Contacts VC,它从一个结构派生数据,然后是一个由 3 个数组组成的数组。我有一个数组数组,以便在我的 Tableview 中有部分。
结构联系人{
var name = String()
var cellPhone = String()
var pager = String()
}
var contacts = [
[Contacts(name: "Name", cellPhone: "Number", pager: "Number"),
Contacts(name: "Name", cellPhone: "Number", pager: "Number") ],
[Contacts(name: "Name", cellPhone: "Number", pager: "Number"),
Contacts(name: "Name", cellPhone: "Number", pager: "Number") ],
[Contacts(name: "Name", cellPhone: "Number", pager: "Number"),
Contacts(name: "Name", cellPhone: "Number", pager: "Number") ]]
在我的联系人 TableViewController 中,我添加了一个 SearchBar 并定义了一个新变量。
var filteredContacts = [[Contacts]]()
并添加了这个功能
func updateSearchResults(for searchController: UISearchController) {
if searchController.searchBar.text! == "" || searchController.searchBar.text == nil {
isSearching = false
filteredContacts = contacts
} else {
// Filter the results
isSearching = true
filteredContacts = [contacts.joined().filter { $0.name.lowercased().contains(searchController.searchBar.text!.lowercased()) }]
}
self.tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return myTitles.count
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return filteredContacts.count
}
return contacts[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! TableViewCell
if isSearching {
cell.column1.text = self.filteredContacts[indexPath.section][indexPath.row].name
cell.column2.text = self.filteredContacts[indexPath.section][indexPath.row].cellPhone
cell.column3.text = self.filteredContacts[indexPath.section][indexPath.row].pager
} else {
cell.column1.text = contacts[indexPath.section][indexPath.row].name
cell.column2.text = contacts[indexPath.section][indexPath.row].cellPhone
cell.column3.text = contacts[indexPath.section][indexPath.row].pager
}
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return myTitles[section]
}
解决方案
@thecloud_of_unknowing
我接受了你所说的并添加了这个:
override func numberOfSections(in tableView: UITableView) -> Int {
if isSearching {
return filteredContacts.count
}
return contacts.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return filteredContacts[section].count
}
return contacts[section].count
}
整个事情中最困难的部分是弄清楚 searchBar 更新函数的语法以及如何过滤嵌套数组。所以以防万一其他人想知道。老实说,我仍然不完全理解这一切是如何运作的,但我会到达那里的。
部分标题没有改变,但至少我可以搜索。
filteredContacts = [contacts.joined().filter { $0.name.lowercased().contains(searchController.searchBar.text!.lowercased()) }]
推荐阅读
- mapquest - MapQuest 的单行地址
- javascript - 如何通过更改asp.net mvc下拉菜单中的选项来过滤html表中的数据?
- amazon-redshift - 如何停止 AWS Redshift 调整大小活动?
- reactjs - 在反应中使用多个样式属性
- php - laravel api ajax表单不会提交
- python - 同时在 Pandas 数据框中进行多项操作
- python - 从列 A 获取列名,然后将该列的值保存在新列 C 中
- botframework - 如何使用 Microsoft bot 框架 SDK3 C# 在 Microsoft 团队中显示欢迎消息
- react-native - 嵌套导航器,可以在所有导航器屏幕中的父组件或固定组件内工作
- python - 安装 Anaconda 3.6 后无法启动 Spyder