Add search bar to table view with data with sections

I have data with a section. I followed the instructions to add a search bar but I am running into errors due to my sections.

My data model looks like this:


struct ActionResult: Codable {

    let data3: [Datum]

}



struct Datum: Codable {

    let actionGoal, actionGoalDescription, actionGoalImage: String

    let actions: [Action]

}



struct Action: Codable {

    let actionTitle: String

    let actionID: Int

    let select, completed, favorite: Bool

    let actionType, actionDescription, actionTips, actionImage: String

    let actionSponsor, actionSponsorURL: String

My table view code looks like this:


import SwiftUI



class ActionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {

    

    @IBOutlet weak var ActionTableView: UITableView!

    @IBOutlet weak var SearchBar: UISearchBar!

    

    var result: ActionResult?

    var index = 0

    var filteredData: [String]?

    

    private let tableView: UITableView = {

        let table = UITableView(frame: .zero,

                                style: .grouped)

        table.register(UITableViewCell.self, forCellReuseIdentifier: "ActionCell")

        return table

    }()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        parseJSON()

        view.addSubview(tableView)

        self.tableView.frame = view.bounds

        self.tableView.delegate = self

        self.tableView.dataSource = self

        SearchBar.delegate = self

        filteredData = result?.data3.actions

The last line has the error code: Value of type [Datum] has no member actions.

The code for the search bar looks like this:


    

        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

        

            filteredData = []

            

            if searchText == "" {

                filteredData = result?.data3.actions

                

            }

            else {

                for action in result?.data3.actions {

                if action.lowercase().contains(searchText.lowercased()) {

                    filteredData.append(action)

                }

            }

        }

            self.tableView.reloadData()

    }

And this code has the same errors.

Value of type [Datum] has no member actions.

I need help how to declare that my data model has sections.

Thanks!

You already asked a very similar question, with same reply.

I suspect the error is here:

filteredData = result?.data3.actions
  • data3 is an array [Datum]
  • actions is a property of Datum, so of each item in the array

So, you have to write:

result?.data3[someIndex].actions

and

                for action in result?.data3[someIndex].actions {

I understand you want to build an array from data3, extracting actionTitle

If so, use map:

someFilteredData = allActions.data3[someIndex].actions.map() { $0.actionTitle }

someIndex may be the indexPath.section of selecteRow

or you can

  • loop with some index from 0 to the number of items in data3 - 1
  • append the result (someFilteredData) to filteredData, to build the array of array of String
Add search bar to table view with data with sections
 
 
Q