Certain Letters cause Index Out of Range CollectionView/TableView UISearchController - ios

I have a collectionview inside a tableview and trying to search XML data. My searchcontroller is on my main tableview's view controller and the results are shown on a separate search results view controller. Some letters will search without any problems. I am getting the following index out of range error however, when I search certain letters:
I debugged and found out the filtered data is sometimes different from the returned/showing data:
I tried to add different conditions to handle the different counts, but I either get a crash for the index in cellForItemAt or a crash at didSelectItemAt.
My code:
Main View Controller:
var filteredData = [AgeRangeData]()
func updateSearchResults(for searchController: UISearchController) {
filteredData.removeAll()
for missingFilteredData in allAgesArray {
filteredData.append(missingFilteredData)
}
if let searchText = searchController.searchBar.text {
filteredData = allAgesArray.compactMap {
var filterObjects = $0
filterObjects.ageRangeData = $0.ageRangeData.filter {
$0.title.range(of: searchText, options: .caseInsensitive) != nil
}
return filterObjects.ageRangeData.isEmpty ? nil : filterObjects
}
let searchResultsVC = searchController.searchResultsController as! SearchResultsVC
searchResultsVC.missingFilteredData = filteredData
DispatchQueue.main.async {
searchResultsVC.resultsTableView.reloadData()
}
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
self.searchBarActive = false
} else {
self.searchBarActive = true
updateSearchResults(for: searchController)
}
DispatchQueue.main.async {
self.missingTableView?.reloadData()
}
}
Search Results View Controller:
var missingKidData: [AgeRangeData] = []
var missingFilteredData: [AgeRangeData]? = []
var missingKidSearchData: AgeRangeData!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return missingFilteredData?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchResultsCell", for: indexPath) as! SearchResultsTableViewCell
if (missingFilteredData?.count)! > indexPath.row {
let missingSearchKidData = missingFilteredData![indexPath.row].ageRangeData
cell.missingKidData = missingSearchKidData[indexPath.row]
cell.missingResultsImage.layer.cornerRadius = 5.0
cell.missingResultsImage.clipsToBounds = true
return cell
}
return UITableViewCell()
}

Related

Search bar returning only first element from data array

currently I am trying to implement my search bar, but something is wrong and I can't figure it out what it is. Here is the code, and explanation.
//global variable for empty array, its type of Any cause I am getting data from network call
var filteredData: [Any]!
//these are my models, which I am using to display them on screen after mapping in network function
var bookedTrips: [BookedTripsForView]?
func viewDidLoad() {
super.viewDidLoad()
filteredData = bookedTrips
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchBar.becomeFirstResponder()
filteredData = []
if searchText == "" {
filteredData = bookedTrips
}else {
for trip in (bookedTrips)! {
if trip.tripName.lowercased().contains(searchText.lowercased()){
filteredData.append(trip)
//if I type, lets say Barcelona, in console its printed correct result,
//but its displaying only first trip in my array, which is Berlin
print("filteredDataArray after appending print: \(String(describing: filteredData))")
}
}
}
self.tableView.reloadData()
}
I hope that my explanation is ok, if something's not clear, I will refactor my question. Thanks in advance.
Here is picture of my screen and console
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let filter = filteredData {
return filter.count
} else if let data = bookedTrips {
return data.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Cells.tripInfo) as! TripsListDetailCell
if let trips = bookedTrips?[indexPath.row] {
cell.configure(trips: trips)
}
return cell
}
Short and simple (One line filter)
var filteredData = [BookedTripsForView]()
var bookedTrips = [BookedTripsForView]()
override func viewDidLoad() {
super.viewDidLoad()
bookedTrips = fetchFromAPIorDB()
filteredData = bookedTrips
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.filteredData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Cells.tripInfo) as! TripsListDetailCell
cell.configure(trips: filteredData[indexPath.row])
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
filteredData = bookedTrips
}
else {
filteredData = bookedTrips.filter({ $0.tripName.lowercased().contains(searchText.lowercased()) })
}
self.tableView.reloadData()
}
var isSearching: Bool = false // As global variable
var bookedTrips: [BookedTripsForView]? = []
var filteredData: [BookedTripsForView]? = []
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchBar.becomeFirstResponder()
filteredData = []
if searchText == "" {
isSearching = false
}else {
isSearching = true
filteredData = bookedTrips.filter { (trip) -> Bool in
if trip.tripName.lowercased().contains(searchText.lowercased()){
return true
}
return false
}
}
self.tableView.reloadData()
}
//Tableview delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return self.filteredData.count
}
return bookedTrips.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Cells.tripInfo) as! TripsListDetailCell
if isSearching {
cell.configure(trips: filteredData[indexPath.row])
}
else {
cell.configure(trips: bookedTrips[indexPath.row])
}
return cell
}
create enum for page mood like this for readable code:
enum PageMood {
case normal
case search
}
and create variable
var pageMode: PageMood = .normal
set normal for first first if search and change pageMode to search like this:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchBar.becomeFirstResponder()
if searchText == "" {
pageMode = .normal
}else {
pageMode = .search
filteredData = bookedTrips?.filter({ item -> Bool in
return (item.tripName?.lowercased().contains(searchText.lowercased()) ?? false)
})
}
self.tableView.reloadData()
}
change define datasource like this:
var bookedTrips: [BookedTripsForView]?
var filteredData: [BookedTripsForView]?
and inside set numberOfItem:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if pageMode == .search {
return filteredData.count ?? 0
} else {
return bookedTrips.count ?? 0
}
}
if only one item findŲŒ Maybe your data has only one item similar to the search text.
And now, since I changed my variables to this :
var filteredData = [BookedTripsForView]()
var bookedTrips = [BookedTripsForView]()
I have one more problem in sections, added comment inside
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: Cells.sectionTitle) as! TripsListHeaderCell
if isSearching {
cell.configure(trips: filteredData[section])
}
else {
cell.configure(trips: bookedTrips[section])
}
return cell
}
How should I implement function viewForHeaderInSection? In response inside every trip I get status of trip (current, upcoming, previous). I would like to sort them by status. If I put this inside viewForHeaderInSection :
if isSearching {
cell.configure(trips: filteredData[section])
} else {
cell.configure(trips:bookedTrips[section])
}
return cell
I get index out of range on bookedTrips[section] If i comment that line, it works until I make mistake in search bar, lets say instead of Barcelona I type Bars, it throws error on filteredData[section] index out of range
In my response, every trip have trip status property which has type string, can I even sort them by that property?

Search bar in Swift not searching with a PHP file

I have no errors in my code but there's definitely something missing. Also there when I press done in the keyboard nothing happens. It might have to do with the search bar function or the table view function. I also have a Search.swift file and I will add it. Any suggestions would be very helpful, I feel really stuck.
SearchBarViewController:
let url = URL(string: "http://127.0.0.1/musicfiles/search.php")
var filteredData = [String]()
var shouldShowSearchResults = false
var search: [Search] = []
var filePath = "http://127.0.0.1/musicfiles/search.php"
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
let task = URLSession.shared.dataTask(with: url!) { (data, snapshot, error) in
let retrievedList = String(data: data!, encoding: String.Encoding.utf8)
print(retrievedList!)
self.parseSongs(data: retrievedList!)
}
task.resume()
}
func parseSongs (data: String) {
if (data.contains("*")) {
let dataArray = (data as String).split(separator: "*").map(String.init)
for item in dataArray {
let itemData = item.split(separator: ",").map(String.init)
let searchSong = Search(songname: itemData[0])
search.append(searchSong!)
for s in search {
print(s.searchSongName())
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return search.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DataCell
let song = search[indexPath.row].searchSongName()
cell.congigureCell(text: song)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let searchSong = search[indexPath.row].searchSongName()
let fileURLString = "\(filePath)\(searchSong)"
print(fileURLString)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
shouldShowSearchResults = false
view.endEditing(true)
filteredData.removeAll()
} else {
shouldShowSearchResults = true
filteredData = search.filter({ (songName) -> Bool in
songName.searchSongName().range(of: searchText) != nil
})
.map { $0.songname }
}
tableView.reloadData()
}
You are loading your data into an array called search.
When you filter your data, you are placing the filtered data into an array called filteredData.
Your tableview always shows the contents of search, so you never see the results of your filtering.
You could check whether filteredData is empty and then return data from that array or search in numberOfRows and cellForRow. Personally I would always use filteredData and make sure that it holds the contents of search when the filter string is empty.
var filteredData = [Search]()
func parseSongs (data: String) {
if (data.contains("*")) {
let dataArray = (data as String).split(separator: "*").map(String.init)
for item in dataArray {
let itemData = item.split(separator: ",").map(String.init)
let searchSong = Search(songname: itemData[0])
search.append(searchSong!)
for s in search {
print(s.searchSongName())
}
self.filterData = self.search
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DataCell
let song = filteredData[indexPath.row].searchSongName()
cell.congigureCell(text: song)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let searchSong = filteredData[indexPath.row].searchSongName()
let fileURLString = "\(filePath)\(searchSong)"
print(fileURLString)
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if (searchBar.text ?? "").isEmpty {
view.endEditing(true)
filteredData = search
} else {
filteredData = search.filter({ (songName) -> Bool in
songName.searchSongName().range(of: searchText) != nil
})
}
tableView.reloadData()
}

Searchbar filtering issue

Im new to the swift, I am trying to filter name from an array using the search bar in console am getting what I entered in the search bar but filtering with predicate im not getting filtered name...please can anyone help in this issue
var caseListOfBooker:[CaseDetails]=[]
var searchString:String=""
var filteredString = [String]()
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText \(searchText)")
searchString = searchText
updateSearchResults()
tableview.reloadData()
}
func updateSearchResults(){
filteredString.removeAll(keepingCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %#", searchString)
let array = self.caseListOfBooker.filter{$0.person_of_interest.contains(searchString)}
print(array)
if let list=array as? [String]{
filteredString=list
}
print(filteredString)
tableview.reloadData()
}
extension SearchPOIVC : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if filteredString != []{
return filteredString.count
}
else
{
if searchString != "[]" {
return caseListOfBooker.count
}else {
return 0
}
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80.00
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:POIProfileDetailsCell = tableview.dequeueReusableCell(withIdentifier: "POIProfileDetailsCell", for: indexPath) as! POIProfileDetailsCell
if filteredString != []{
cell.poiName.text = filteredString[indexPath.row]
return cell
}else{
if searchString != "[]"{
cell.poiName.text = self.caseListOfBooker[indexPath.row].person_of_interest
}
return cell
}
}
The most efficient way to filter custom classes is to use the same type for the data source array and the filtered array
var caseListOfBooker = [CaseDetails]()
var filteredBooker = [CaseDetails]()
Add a property isFiltering which is set to true when the search text is not empty
var isFiltering = false
and delete searchString and filteredString
var searchString:String=""
var filteredString = [String]()
In updateSearchResults filter the data source array (with native Swift functions), set isFiltering accordingly and reload the table view
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
print("searchText \(searchText)")
updateSearchResults(searchText: searchText)
}
func updateSearchResults(searchText: String) {
if searchText.isEmpty {
filteredBooker.removeAll()
isFiltering = false
} else {
filteredBooker = caseListOfBooker.filter{$0.person_of_interest.range(of: searchText, options: .caseInsensitive) != nil }
isFiltering = true
}
tableview.reloadData()
}
In the table view data source methods display the data depending on isFiltering
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return isFiltering ? filteredBooker.count : caseListOfBooker.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "POIProfileDetailsCell", for: indexPath) as! POIProfileDetailsCell
let booker = isFiltering ? filteredBooker[indexPath.row] : caseListOfBooker[indexPath.row]
cell.poiName.text = booker.person_of_interest
}
let array = self.caseListOfBooker.filter{$0.person_of_interest.contains(searchString)}
You are getting array of CaseDetails objects and trying to cast to array of String
It fails. You need to get string values from the CaseDetails object and join them
Use
filteredString = array.map { $0.person_of_interest }
Or
for caseDetail in array {
filteredString.append(caseDetail.person_of_interest)
}
Instead of
if let list = array as? [String]{
filteredString=list
}
var searchPredicate = NSPredicate()
searchPredicate = NSPredicate(format: "self CONTAINS[C] %#", your_searching_text)
let Final_Search_array = (Your_Array).filtered(using: searchPredicate)

Swift 3/4: SearchBar not filtering results properly in TableView

I have a popup with searchBar at the top and TableView below it. TableView is populated by dynamic data. I have a custom tableViewCell, with a label for names and a checkBox(M13CheckBox Library) to select a name.
Now, when I search for a name, Firstly the tableView is not loaded as the user types a name in the search bar. For eg, Suppose there are persons named "Mary", "Mackenzie", "Margaret" and "Mallory". I want to search for "Margaret", so as I start typing "Mar" in searchBar, then "Mary" and "Margaret" are filtered properly in tableView, but when I go back i.e "Ma", then it should show all the 4 names, since "Ma" is present in the list, But the tableView does not show anything.
So tableView should always reload as user types in searchBar if the letters are contained in the names. Please help me sort this issue. Since it is a popup I am passing data to tableView from another VC, by notification.
Here is my code for search VC:
class ParticipantsListVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{
public static var participantNameArray:[String] = [String]() //global var
var viewController: ViewController!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchParticipantFilter.delegate = self
viewController = ViewController()
let notificationName = NSNotification.Name("reloadList")
NotificationCenter.default.addObserver(forName: notificationName, object: nil, queue: OperationQueue.main) { (notifObject) in
self.tableView.reloadData()
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText:String) {
if searchText == "" {
ParticipantsListVC.participantNameArray.removeAll()
viewController.getParticipantList() // func to get list from sever
}else{
ParticipantsListVC.participantNameArray = ParticipantsListVC.participantNameArray.filter({(name) -> Bool in
return name.lowercased().contains(searchText.lowercased())
})
}
self.tableView.reloadData()
}
}
Also if I select a name, then checkBox is selected in front of that name.But when I click on cancel(X) in searchBar, then always the first cell in tableView is shown selected and not the name that I had selected. I don't know why always the first cell gets selected, after selecting name from filtered list.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ParticipantListCell
let dict = ParticipantsListVC.participantNameArray[indexPath.row]
cell.participantNameLabel.text = dict
if selectedIndexPaths.contains(indexPath) {
cell.selectedParticipantCB.setCheckState(.checked, animated: true)
}else{
cell.selectedParticipantCB.setCheckState(.unchecked, animated: true)
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Since any random cell was getting selected on scrolling So I added this code.
tableView.deselectRow(at: indexPath, animated: true)
if selectedIndexPaths.contains(indexPath) {
selectedIndexPaths.removeObject(object: indexPath)
}else{
selectedIndexPaths.append(indexPath)
}
tableView.reloadData()
}
I don't want to use searchBar in headerView or another tableView to show filtered list. Please much appreciated.Thank you.
You need to create another array to hold the backup of data array.
var arrParticipantList = [String]()
var arrParticipantListBackup = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar.delegate = self
self.tblParticipantList.delegate = self
self.tblParticipantList.dataSource = self
self.arrParticipantList = ["Mary", "Mackenzie", "Margaret", "Mallory","Molly"]
self.arrParticipantListBackup = self.arrParticipantList
}
Code to search for search string, refill array and reload tableview
extension ViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
var searchText = searchBar.text! + text
if range.length > 0 {
if range.location == 0 {
self.arrParticipantList = self.arrParticipantListBackup
self.tblParticipantList.reloadData()
return true
}
searchText = String(searchText.dropLast(range.length))
}
self.arrParticipantList = self.arrParticipantListBackup.filter({$0.lowercased().hasPrefix(searchText.lowercased())})
self.tblParticipantList.reloadData()
return true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.searchBar.text = ""
self.searchBar.resignFirstResponder()
self.arrParticipantList = self.arrParticipantListBackup
self.tblParticipantList.reloadData()
}
}
Code for tableview
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arrParticipantList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = self.arrParticipantList[indexPath.row]
return cell!
}
}
Hope this solves your issue.
struct namelist {
var searchname: NSString
}
var searchActive = Bool()
var newSearchArray = [namelist]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchActive ? newSearchArray.count : nameOldArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Cell:SearchTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "Cell") as! SearchTableViewCell
Cell.selectionStyle = .none
if (searchActive == true) {
if ( newSearchArray.count > 0) {
var para = NSMutableAttributedString()
para = NSMutableAttributedString(string:(newSearchArray[indexPath.row].searchname) as String)
do {
let regex = try NSRegularExpression(pattern: searchText, options: NSRegularExpression.Options.caseInsensitive )
let nsstr = newSearchArray[indexPath.row].searchname
text = nsstr as String
let all = NSRange(location: 0, length: nsstr.length)
var matches : [String] = [String]()
regex.enumerateMatches(in: text, options: NSRegularExpression.MatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let results = nsstr.substring(with: r.range) as String
matches.append(results)
let substringrange = result!.rangeAt(0)
para.addAttribute(NSForegroundColorAttributeName, value:UIColor.init(red: 237/255.0, green: 60/255.0, blue: 58/255.0, alpha: 1.0), range: substringrange)
Cell.namelbl.attributedText = para
}
}
} catch {
}
}
}
else {
Cell.namelbl.text = self.searchname[indexPath.row] as? String
}
return Cell
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchArray.removeAllObjects()
newSearchArray.removeAll()
if Search.text != nil {
for i in 0 ..< searchname.count {
searchText = Search.text!
text = ((searchname.object(at: i))) as! String
if text.lowercased().contains(searchText.lowercased()) {
let elm = namelist(searchname: text as NSString)
self.newSearchArray.append(elm)
}
}
}
searchActive = !newSearchArray.isEmpty
searchBar.resignFirstResponder()
yourTableName.reloadData()
}

Filter result(s) in array of class (Swift 3)

In Class
class Objects {
var number: Int!
var name: String!
init(number: Int, name: String) {
self.number = number
self.name = name
}
}
In viewController
var allObjects = [Objects]()
var inSearchMode = false
#IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
if inSearchMode {
let fill: Objects!
fill = filteredObject[indexPath.row]
cell.configureCell(fill)
} else {
let fill: Objects!
fill = allObjects[indexPath.row]
cell.configureCell(fill)
return cell
}
}
return UITableViewCell()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if inSearchMode {
return filteredObject.count
}
return allObjects.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
inSearchMode = false
tableView.reloadData()
view.endEditing(true)
} else {
inSearchMode = true
var lowerCase = Int(searchBar.text!)
filteredObject = allObjects.filter({$0.number == lowerCase})
tableView.reloadData()
print(filteredObject)
}
}
I would like to have a search bar that filters and shows only one result that contains the number we are looking for. I thought about using contains and put the number we input from the search bar.
I manage to get one object into the filteredObject, but it won't show up in the tableView
Look carefully at this part of your code:
if inSearchMode {
let fill: Objects!
fill = filteredObject[indexPath.row]
cell.configureCell(fill)
} else {
let fill: Objects!
fill = allObjects[indexPath.row]
cell.configureCell(fill)
return cell
}
You did not return a cell when you are in search mode, that is why you do not see anything when searching.
I would use a computed property to drive the tableview; This property is either all the objects or the filtered object(s):
var allObjects = [Objects]()
var filteredObjects: [Objects]?
var objects: [Objects] = {
return filteredObjects ?? allObjects
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.objects.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Cell
let fill = self.objects[indexPath.row]
cell.configureCell(fill)
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
self.filteredObjects = nil
} else {
var lowerCase = Int(searchBar.text!)
self.filteredObjects = allObjects.filter({$0.number == lowerCase})
}
tableView.reloadData()
}

Resources