ios - Swift getting index out of range error in Tableview while searching and scrolling result
问题描述
In myscenario, I am trying to load my JSON data into tableview array and assigning Tableview array to filter array. Here, while searching some time I am getting each and doing scroll to getting crash index out of range
error. I am loading data from JSON with help of codable.
var sections = [Section]()
var filteredSections = [Section]()
func numberOfSections(in tableView: UITableView) -> Int {
return isFiltering ? filteredSections.count : sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let currentSection = isFiltering ? filteredSections[section] : sections[section]
return currentSection.result.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return isFiltering ? filteredSections[section].title : sections[section].title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:HomeCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! HomeCustomCell
let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section]
let item = section.result[indexPath.row] // getting here error
return cell
}
Search Delegates
below delegate methods I am using for Searchbar
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
isFiltering = true
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
//filteredSections.removeAll()
self.tableView.reloadData()
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if searchText == "" {
print("UISearchBar.text cleared!")
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if !searchText.isEmpty {
if (project_type == "project" || project_type == "test_project"){
filteredSections = sections.compactMap { section -> Section? in
let filteredContent = section.result.filter {($0.project_viewable_id?.range(of: searchText, options: .caseInsensitive) != nil))
}
isFiltering = true
} else {
filteredSections = sections.compactMap { section -> Section? in
let filteredContent = section.result.filter {($0.task_viewable_id?.range(of: searchText, options: .caseInsensitive) != nil))
}
isFiltering = true
}
} else {
//filteredSections.removeAll()
isFiltering = false
}
self.tableView.reloadData()
}
解决方案
Make sure to reload UITableView
when you change isFiltering Bool
value.
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
isFiltering = true
self.tableView.reloadData()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
isFiltering = false
self.tableView.reloadData()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
self.tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
isFiltering = false
//filteredSections.removeAll()
self.tableView.reloadData()
}
推荐阅读
- java - Spring Batch remore 分区添加从属运行时
- reactjs - 如何解决无法使用 jest/jsdom 设置未定义错误的属性“_eventListeners”?
- ruby - ruby_block 厨师资源中无法逃脱美元符号
- tsql - TSQL 中 Pivot 中的平均值四舍五入到小数点后两位
- c++ - PCL:如何从 IndicesClusterPtr 中删除特定集群?
- mysql - 正值或负值取决于关系 - 可以在单个语句中选择 SUM(...) 吗?
- react-native - ComponentDidUpdate() 导致 IOS 中的内存泄漏和电池耗尽
- python - 具有多个变量的高斯过程回归:内核的适应
- git - 命令 git reset --hard 后恢复数据
- vue.js - 如何在开发时绑定到未知的属性名称