I added search bar to a table view controller programmatically. I use Firebase. But there is no cell since I added search bar. Please see the code:
var soemArray = [SomeFile]()
var filteredArray = [SomeFile]()
var shouldShowSearchResults = false
var searchController: UISearchController!
var customSearchController: CustomSearchController!
override func viewDidLoad() {
super.viewDidLoad()
configureCustomSearchController()
}
func getData(){
dataBaseRef.child("text").observe(.value, with: { (snapshot) in
var results = [SomeFile]()
for user in snapshot.children {
let user = SomeFile(snapshot: user as! DataSnapshot)
if user.name != user.url {
results.append(user)
}
}
someArray = results.sorted(by: { (u1, u2) -> Bool in
u1.name < u2.name
})
self.tutsArray = results.sorted(by: { (u1, u2) -> Bool in
u1.url! < u2.url!
})
self.tableView.reloadData()
}) { (error) in
print(error.localizedDescription)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
/
if shouldShowSearchResults {
return filteredArray.count
} else {
return tutsArray.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "someCell", for: indexPath) as! ArtTutsTableViewCell
cell.name.text = someArray[indexPath.row].tutName
/
if shouldShowSearchResults {
cell.name.text = filteredArray[indexPath.row].name
} else {
cell.name.text = someArray[indexPath.row].name
}
return cell
}
func configureSearchController() {
/
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self as? UISearchResultsUpdating
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.delegate = self as? UISearchBarDelegate
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
}
configureCustomSearchController() {
customSearchController = CustomSearchController(searchResultsController: self, searchBarFrame: CGRect(x:0.0, y:0.0, width: tableView.frame.size.width, height:50.0), searchBarFont: UIFont(name: "Futura", size: 16.0)!, searchBarTextColor: UIColor.orange, searchBarTintColor: UIColor.black)
/
tableView.tableHeaderView = customSearchController.customSearchBar
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
shouldShowSearchResults = true
tableView.reloadData()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
shouldShowSearchResults = false
tableView.reloadData()
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
if !shouldShowSearchResults {
shouldShowSearchResults = true
tableView.reloadData()
}
searchController.searchBar.resignFirstResponder()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
guard let searchString = searchController.searchBar.text else {
return
}
/
self.filteredArray = self.tutsArray.filter({ (tutArray: Tutorials) -> Bool in
let text: NSString = tutArray.tutName
as! NSString
return (text.range(of: searchString, options: NSString.CompareOptions.caseInsensitive).location) != NSNotFound
})
self.tableView.reloadData()
}
func didStartSearching() {
shouldShowSearchResults = true
tableView.reloadData()
}
func didTapOnSearchButton() {
if !shouldShowSearchResults {
shouldShowSearchResults = true
tableView.reloadData()
}
}
func didTapOnCancelButton() {
shouldShowSearchResults = false
tableView.reloadData()
}
func didChangeSearchText(searchText: String) {
self.filteredArray = self.tutsArray.filter({ (tutArray: Tutorials) -> Bool in
let text: NSString = tutArray.tutName as NSString
return (text.range(of: searchText, options: NSString.CompareOptions.caseInsensitive).location) != NSNotFound
})
tableView.reloadData()
}