I use the below to filter my Custom Object Array using a SearchBar.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
if searchText != ""
{
searchedData = data.filter({
$0.reg?.range(of: searchText, options: [.caseInsensitive, .diacriticInsensitive]) != nil
|| $0.type?.range(of: searchText, options: [.caseInsensitive, .diacriticInsensitive]) != nil
|| $0.type?.removingSymbols().range(of: searchText, options: [.caseInsensitive, .diacriticInsensitive]) != nil
|| $0.reg?.removingSymbols().range(of: searchText, options: [.caseInsensitive, .diacriticInsensitive]) != nil
})
searching = true
table1.reloadData()
}
else
{
searching = false
table1.reloadData()
}
}
For the below Class:
class CustomIndex {
let id: Int64
let type: String?
let reg: String?
init(id: Int64, type: String, reg: String)
{
self.id = id
self.type = type
self.reg = reg
}
}
var data: [CustomIndex] = [CustomIndex]()
var searchedData: [CustomIndex] = [CustomIndex]()
var searching = false
@IBOutlet weak var dataSearch: UISearchBar!
I need to add a SearchBar to my other table which works the same however my Class Contains an Array which fills the rows and the initial array is for the sections. So i'm not quite sure how to implement this to keep the Sections that contain the text within the section and filter the Array that is within. This is the nested class array:
class RowIndex {
let date: Date?
let type: String?
let reg: String?
init(date: Date, type: String, reg: String) {
self.date = date
self.type = type
self.reg = reg
}
}
class SectionIndex {
let index: Int?
let title: String?
let date: Date?
var data: [RowIndex] = [RowIndex]()
var minutes: Int?
init(index: Int, title: String, date: Date, data: [RowIndex], minutes: Int)
{
self.index = index
self.title = title
self.date = date
self.data = data
self.minutes = minutes
}
}
Which is used for my table:
var array: [SectionIndex] = [SectionIndex]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array[section].data.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return array.count
}