首页 > 解决方案 > 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()
    }

标签: iosswift

解决方案


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()
}

推荐阅读