0

I have a search function in my app which gets data from a fetchedResultsController. The problem is when I display the filtered data in the tableView everytime it gets to a new section, the array starts over and over (due to indexPath.row being 0 everytime indexPath.section increments). I've never been good at nested arrays and I thought this is the perfect time to learn them, since I can't get over my problem without this array.

So I have this array which is the filtered data out of the fetchedResultsController:

filteredItems = (fetchedResultsController.fetchedObjects?.filter({(budget : Budget) -> Bool in
 return (budget.dataDescription?.lowercased().contains(searchText.lowercased()))!
        }))!

How can I make an array called filteredObjects which sorts my items for sections? For example

- Section 1 ( filteredObjects[0] ):

  • item 1 ( filteredObjects[0][1] )
  • item 2 ( filteredObjects[0][2] )

-

Section 2 ( filteredObjects[1] ):

  • item 1 ( filteredObjects[1][0] ) etc

2 Answers 2

1

Use below method to define number of sections

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return filteredObjects.count
}

And, for number of rows

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return filteredObjects[section].count
    }

And Finally, for CellForRowAtIndexpath

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TaskTableViewCell
    let currentItem = filteredObjects[indexPath.section][indexPath.row]

    ... Here, use currentItem as whatever it is (Object or dictionary)

            return cell
}
Sign up to request clarification or add additional context in comments.

2 Comments

But how do I create filteredObjects out of filteredItems, knowing that filteredItems is a single array ( var filteredItems = [Budget]())
What are the parameters/Results of filteredItems? is it array?
0
    let budgets = [Budget]()
    let searchResult = [Budget]()

You should always use searchResult array in tableview datasource methods. initially you should add all objects of budgets array to searchResult & load table view.

when search started remove all items from searchResult and add filtered result to search result array & reload tableview.

    func search(searchText:String){
        searchResult.removeAll()
        let result = budgets.filter({
            var budget = $0
            let filtered = budget.expenses.filter({
                if let deptName = $0.deptName{
                    return deptName.lowercased().contains(searchText.lowercased())
                }
                return false
            })
            budget.expenses = filtered
            return budget.expenses.count != 0
        })

        searchResult.append(contentsOf: result)
    }

struct Budget {
    var expenses = [Expense]()
 }

 struct Expense{
    var deptName:String?
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.