I have tried to set the searchBar
as a tableHeaderView
inside of the viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
// SearchController initializiation
self.searchController = UISearchController.init(searchResultsController: nil)
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.sizeToFit()
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.barTintColor = UIColor.white
self.searchController.searchBar.keyboardAppearance = .default
self.searchController.searchBar.backgroundColor = UIColor.white
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.obscuresBackgroundDuringPresentation = false
self.tableView.tableHeaderView = self.searchController.searchBar
self.definesPresentationContext = true
self.fetch()
self.tableView.reloadData()
}
And this my fetch()
function:
func fetch() {
let fetchRequest:NSFetchRequest<Phone> = Phone.fetchRequest()
fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "header", ascending: true), NSSortDescriptor.init(key: "date", ascending: true)]
self.fetchedResultsController = NSFetchedResultsController.init(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: "header", cacheName: nil)
self.fetchedResultsController.delegate = self
do {
try self.fetchedResultsController.performFetch()
self.tableView.reloadData()
} catch {}
}
But I don't understand they are doesn't work. The Xcode crashes and nothing actually happens. And then I have tried to change something inside of viewDidLoad
method:
override func viewDidLoad() {
super.viewDidLoad()
self.fetch()
self.tableView.reloadData()
// SearchController initializiation
self.searchController = UISearchController.init(searchResultsController: nil)
self.searchController.delegate = self
self.searchController.searchBar.delegate = self
self.searchController.searchBar.sizeToFit()
self.searchController.searchResultsUpdater = self
self.searchController.searchBar.barTintColor = UIColor.white
self.searchController.searchBar.keyboardAppearance = .default
self.searchController.searchBar.backgroundColor = UIColor.white
self.searchController.hidesNavigationBarDuringPresentation = true
self.searchController.obscuresBackgroundDuringPresentation = false
self.tableView.tableHeaderView = self.searchController.searchBar
self.definesPresentationContext = true
}
Success! Works properly. I don't understand what is the difference?
You should try removing the self.tableView.reloadData()
from the viewDidLoad()
method, because the data wasn't even loaded to be reloaded.
It is because you set the delegates for searchResultsUpdater and searchBar before even your fetchedResultsController is initialised. Some where in your code searchResultsUpdater or searchBar's delegate methods are accessing your fetchedResultsController and getting it nil so you app crashes. My suggestion is to avoid the crash is to make searchResultsController as computed or lazy property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With