ios - sectionIndexTitles 不工作
问题描述
我在我的项目中添加了下一个代码:
let arrIndexSection = ["A","B","C","D", "E", "F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return arrIndexSection
}
但是在单击任何字母后,tableview 只会滚动到我的部分的 B 字母。
我的 TableViewController 的完整代码:
class TagsController: UITableViewController {
var hashtags = [HashTags]()
var filtered = [HashTags]()
let searchController = UISearchController(searchResultsController: nil)
var searchBarIsEmpty: Bool {
guard let text = searchController.searchBar.text else { return false }
return text.isEmpty
}
var isFiltering: Bool {
return searchController.isActive && !searchBarIsEmpty
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 135
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
definesPresentationContext = true
}
// MARK: - Table view data source
let arrIndexSection = ["A","B","C","D", "E", "F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return arrIndexSection
}
override func numberOfSections(in tableView: UITableView) -> Int {
if isFiltering {
return filtered.count
}
return hashtags.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if isFiltering {
return filtered[section].title
}
return hashtags[section].title
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Tags", for: indexPath)
var item = HashTags()
if isFiltering {
item = filtered[indexPath.section]
} else {
item = hashtags[indexPath.section]
}
let tags = item.tags
cell.textLabel?.text = tags.joined(separator: ", ")
return cell
}
}
extension TagsController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
filterContentForSearchText(searchController.searchBar.text!)
}
func filterContentForSearchText(_ searchText: String) {
filtered = hashtags.filter({ (hashtags: HashTags) in return hashtags.tags.contains(where: { (aTag: String) in aTag.contains(searchText.lowercased()) }) })
tableView.reloadData()
}
}
问题可能出在哪里?
解决方案
除非您始终按字母顺序将 26 个部分完全分组,否则您的代码毫无意义。那可能性有多大?你有一段以 X 开头的单词?即使在过滤时??我对此表示怀疑。
一般不要硬编码arrIndexSection
。相反,sectionIndexTitles
根据当时实际存在的部分动态返回结果,就像您为numberOfSections
. 每个部分一个sectionIndexTitles
数组元素。
推荐阅读
- python - 从 csv 输出中删除科学记数法
- node.js - NodeJS 中的 DIGEST-MD5 实现
- c - C - valgrind:未初始化的值与 printf
- python - 循环遍历数据框行以进行 curl
- kubernetes - EKS Ingress ALB 为某些服务添加 HTTP 侦听器,为其他服务添加 HTTPS
- javascript - 从数据库获取数据后,接下来如何使用 JS 将数据显示到 HTML 上?
- android - 如何验证付费应用没有退款
- javascript - 在 Javascript 中定义正则表达式
- c# - C# 等价于 __builtin_expect
- java - MySql为什么返回**Long**,如何获取**Float**