swift - 问题过滤带有部分的 Swift UITableView - 未显示结果
问题描述
本质上,我有一个表格视图,它显示了一组名称并将它们分成多个部分 - 我最近添加了一个搜索栏来过滤数据,但我在显示过滤后的数据时遇到了问题,我没有收到任何错误,只是一个空数组集.
以下是我呈现/尝试过滤数据的方式:
var sections = [customerListSection]()
var structure = [customerList]()
var filteredSections = [customerListSection]()
var filteredStructure = [customerList]()
func filterContentForSearchText(_ searchText: String, scope: String = "All") {
filteredStructure = structure.filter({( customers : customerList) -> Bool in
return customers.customer.lowercased().contains(searchText.lowercased())
})
tableView.reloadData()
}
从我的测试来看,No Customers
每次我尝试使用搜索时都会打印出来,这让我认为filteredSections 没有被更新。有什么想法吗?见下文:
override func numberOfSections(in tableView: UITableView) -> Int {
if isFiltering() {
if filteredSections.count == 0 {
print("No Customers")
} else {
tableView.restore()
}
return filteredSections.count
}
if sections.count == 0 {
} else {
tableView.restore()
}
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
return filteredSections[section].items.count
}
return sections[section].items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
//let item = sections[indexPath.section].items[indexPath.row]
let customerList: customerList
if isFiltering() {
customerList = filteredSections[indexPath.section].items[indexPath.row]
cell.textLabel!.text = customerList.customer
} else {
customerList = sections[indexPath.section].items[indexPath.row]
cell.textLabel!.text = customerList.customer
}
return cell
}
结构:
struct customerListSection {
let customerType : String
var items : [customerList]
}
struct customerList: Decodable {
let customerid: Int
let customer: String
let type: String
}
这就是我使用 json 填充 tableview 的方式:
private func fetchJSON() {
guard let url = URL(string: "server.com")
else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "value=1".data(using: .utf8)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data else { return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let res = try decoder.decode([customerList].self, from: data)
let grouped = Dictionary(grouping: res, by: { $0.type })
var keys = grouped.keys.sorted()
if let index = keys.firstIndex(of: "Display On Top") {
let first = keys.remove(at: index)
keys.insert(first, at: 0)
}
self.sections = keys.map({customerListSection(customerType: $0, items: grouped[$0]!)})
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
catch {
print(error)
}
}.resume()
}
解决方案
推荐阅读
- selenium - 无法在我的本地 Windows 代理上运行 teamcity gradle build:
- android - 如何在颤动中解析日期时间
- android-studio - 卡在 Android drawable importer
- android - 当我刷新我的页面列表时,我在 PageAdapter 中的项目将在短时间内放错位置
- python - 我想知道如何使用 Bot framework SDK for python 设置自适应卡的主机配置
- oracle - Oracle 19c 和“没有为主机返回有效的 IP 地址”
- python - 如何自动化循环日期以便在当前日期后自动添加一个月?[Python-Django]
- php - 使用 eloquent laravel 获取连接表数据
- node.js - 如何在 ubuntu 上设置 webRTC 服务器
- laravel - 如何在 Laravel 6 中对 restAPI 执行具有许多参数的存储过程