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!