这里是业余的。我真的很想能够通过我创建的联系人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") ]]
在我的Contacts 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]
}
答案 0 :(得分:0)
@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()) }]