How to filter and search with grouped tableview?

Arnav GUPTA Source

I currently have 2 arrays

expensesRecieved which is an array of type of array of expenses. so something like this [[Expense]].

This contains a list of grouped expenses each associated with a date and the dates are stored in a separate array called expensesKeys which is of type string.

I am trying to run a searchBar on this, which would allow me to only display expenses which contain the word searched and so far have this code:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == ""{
        isSearching = false
        view.endEditing(true)
        tableView.reloadData()
    }
    else {
        isSearching = true
        for i in 0..<expensesRecieved.count{
            for e in 0..<expensesRecieved[i].count{
                if ((expensesRecieved[i][e].name?.contains(searchBar.text!))! || expensesRecieved[i][e].categories!.title!.contains(searchBar.text!) || expensesRecieved[i][e].collections!.title!.contains(searchBar.text!)) {

                    filteredExpenses[i].append(expensesRecieved[i][e])

                }

            }
        }
        tableView.reloadData()

    }
}

and then in the cellForRowAt method, I have the following code

if isSearching{
        expense = self.filteredExpenses[indexPath.section][indexPath.row]
    }
    else{
        expense = self.expensesRecieved[indexPath.section][indexPath.row]

    }

however, I get a thread bad EXE on the second line.

I have done search filtering before, but that was without a grouped tableview, so that is why I am confused.

iosswiftuitableviewuisearchbar

Answers

comments powered by Disqus