I know there's been questions like this before: here or here
But it hasn't gone into much detail, as much as I'd like anyway. I have this 'Explore' page screen sort of thing. It has a search bar at the top and I'd like to show content/posts in the middle section of it. But when the user clicks on the search bar, I want go to a tableview controller, replacing the content on the screen, to show the search results.
I had a tableviewcontroller embedded in the UIView container:
import UIKit
import Firebase
class SearchTableViewController: UITableViewController, UISearchResultsUpdating {
let searchController = UISearchController(searchResultsController: nil)
#IBOutlet var searchResultsTableView: UITableView!
var usersArray = [NSDictionary?]()
var filteredIsers = [NSDictionary?]()
var ref = Database.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
ref.child("users").child("public").queryOrdered(byChild: "username").observe(.childAdded, with: { (snapshot) in
if let user = snapshot.value as? [String:Any]{
let username = user["username"] as? String ?? ""
if(username == SpalshScreenViewController.UserData.username){
return
}else{
self.usersArray.append(snapshot.value as? NSDictionary)
}
}else{
return
}
print(self.usersArray)
// insert rows
self.searchResultsTableView.insertRows(at: [IndexPath(row: self.usersArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic )
})
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if searchController.isActive && searchController.searchBar.text != "" {
return filteredIsers.count
}else{
return self.usersArray.count
}
}
func updateSearchResults(for searchController: UISearchController) {
filterContent(searchText: self.searchController.searchBar.text!)
}
func filterContent(searchText: String){
self.filteredIsers = self.usersArray.filter{ user in
let username = user!["username"] as? String
return(username?.lowercased().contains(searchText.lowercased()))!
}
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let user : NSDictionary?
if searchController.isActive && searchController.searchBar.text != "" {
user = filteredIsers[indexPath.row]
}else{
user = self.usersArray[indexPath.row]
}
cell.textLabel?.text = user?["firstName"] as? String
cell.detailTextLabel?.text = user?["username"] as? String
let profilePicUrl = user?["profilePicURL"] as? String
let encodedurl = URL(string: profilePicUrl!)
print(profilePicUrl)
cell.imageView!.kf.setImage(with: encodedurl, placeholder: UIImage(named: "profile_pic"))
return cell
}
}
Would I have that code in it's separate UITableViewController and then present that view when the search bar is clicked or?
I know you'd change the let searchController = UISearchController(searchResultsController: nil) to something like let searchController = UISearchController(searchResultsController: tableViewController)
but I don't know how to tie everything in. In my searchViewController I have reference to the UISearchBar, I was thinking I'd add a tap gesture onto it and present a the tablecontrollerview when it's clicked but that would mean there would be an inconsistency in animation and search bar style?
EDIT:
So basically, I have a view controller, in that view controller I have a search bar at the top (doing nothing at the moment) and underneath that search bar will be a collection view full of different posts. When the user clicks on the search bar I'd like to replace that collection view with a table view with the results of the search result. How would I achieve that?
You should set up searchResultsController and searchResultsUpdater properties of the UISearchController class
Concerning UISearchBar object, you have to use the one which comes with UISearchController and add it programmatically to a view.
view.addSubview(searchController.searchBar)
Hey #nathan if you can please explain in more details than it would be better. Other wise on base of my current understanding. If you have search bar added in top of view and you have the table view in it also then you can update your table view in search bar delegate methods. I am sharing the search bar delegate methods for your help.
extension SearchViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let str = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
viewModel?.search(text: str)
tableView.reloadData()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let t = searchBar.text?.trimmingCharacters(in: .whitespacesAndNewlines) {
if t.count > 0 {
viewModel?.addNewSearch(text: t)
tableView.reloadData()
}
}
searchBar.resignFirstResponder()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
viewModel?.cancel()
navigationController?.popViewController(animated: true)
} }
I have used MVVM in my code so ViewModel is basically used for data update and changes. Thanks you can ask if you have any confusion.
Hoping you can help me out...I've been trying to figure this issue out for a couple days now.
I'm using Parse (www.parse.com) as my backend, and have hosted it on my own AWS server.
Structure of the app:
In AppDelegate, if user is logged in, show a ViewController that sets up my SlideMenuControllerSwift (https://github.com/dekatotoro/SlideMenuControllerSwift) and my TabBarController.
[Storyboard][1]
In my tab bar controller, I have a navigation controller that leads to a UITableViewController that segues to another UITableViewController when I click on a row.
Problem:
http://imgur.com/izdCBgt
1) I click on a row and it performs an asynch query to my parse database
2) The data populates the table and then disappears
3) If I change the tab and go back to the main tab, the data reappears
Also
1) I click on a row and it performs an asynch query to my parse database
2) The data populates the table and then disappears
3) If I go back to the original UITableViewController, it does not transition back properly, and I need to change tabs back and forth until it reappears
Code:
I segue to the documents table view controller using the storyboard segue. Here is the relevant code in my DocumentsTableViewController:
class DocumentTableViewController: UITableViewController, UIDocumentInteractionControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MMCropDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("initDocs")
}
var specificDocs=[Document](){
didSet{
dispatch_async(dispatch_get_main_queue(), {
//we might be called from the parse block which executes in seperate thread
self.loadView()
})
}
}
override func viewDidLoad() {
tableView.dataSource = self
tableView.delegate = self
print("we loadeD")
super.viewDidLoad()
navigationItem.title = "Job Documents"
let cameraButton = UIBarButtonItem(image: UIImage(named: "Camera"), style: .Plain, target: self, action: #selector(self.didPressCamera(_:)))
navigationItem.rightBarButtonItem = cameraButton
self.queryForTable()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
}
// Define the query that will provide the data for the table view
func queryForTable() {
// Run a spinner to show a task in progress
let progressHUD = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
progressHUD.label.text = "Loading..."
//2 using those jobActions to find which documents are mine
let query = PFQuery(className: Document.parseClassName())
query.whereKey(Document.jobActionCol(), containedIn: jobActions)
query.includeKey(Document.documentCategoryCol())
// do {
// let test = try query.findObjects()
// self.specificDocs = test as! [Document]
// progressHUD.hideAnimated(true)
// } catch {
// print("error!")
// }
// FIND WHY THIS DOESNT WORK....WHAT THE F
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
self.specificDocs = objects as! [Document]
print("done")
progressHUD.hideAnimated(true)
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return specificDocs.count
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cellIdentifier")
if ((cell) == nil){
cell = UITableViewCell.init(style: .Default, reuseIdentifier: "cellIdentifier")
}
cell!.textLabel?.text = specificDocs[indexPath.row].documentCategory!.type!
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
// print(UIApplication.sharedApplication().keyWindow?.performSelector("recursiveDescription"))
return cell!
}
deinit {
print("docs deinit")
}
}
Oh, now I see intention of your codes. But, Do you have some reason that doing? It isn't right way that directly call loadView().
tableView.dataSource = self
tableView.delegate = self
move to first in viewWillAppear and, It will be work except too fast done to receive from parse.
Normal way will be like below:
var specificDocs=[Document](){
didSet{
dispatch_async(dispatch_get_main_queue(), {
//we might be called from the parse block which executes in seperate thread
self.loadView()
})
}
self.loadView() to self.tableView.reloadData().
tableView.dataSource = self
tableView.delegate = self
That doesn't needs.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()}
That doesn't needs too.
Anyway, your codes will work just modify calling self.loadView() to self.tableView.reloadData()
I have two table views. One which the user clicks on and one where data is displayed. When the user clicks on a cell in the first table view a query is made to my firebase database and the query is stored in an Array. I then pass the data through a segue. I used a property observer so I know that the variable is being set. By using break points I was able to determine that my variable obtains its value right before the cellForRowAtIndexPath method. I need help displaying the data in my table view. I do not know where to reload the data to get the table view to update with my data. I am using Swift.
EDIT 2: I have solved my problem. I will post my first and second table views so that you can see my solution.
FirstTableView
import UIKit
import Firebase
import FirebaseDatabase
class GenreTableViewController: UITableViewController {
let dataBase = FIRDatabase.database()
var genreArray = ["Drama","Classic,Comic/Graphic novel","Crime/Detective","Fable,Fairy tale","Fantasy","Fiction narrative", "Fiction in verse","Folklore","Historical fiction","Horror","Humour","Legend","Magical realism","Metafiction","Mystery","Mythology","Mythopoeia","Realistic fiction","Science fiction","Short story","Suspense/Thriller","Tall tale","Western,Biography","Autobiography","Essay","Narrative nonfiction/Personal narrative","Memoir","Speech","Textbook","Reference book","Self-help book","Journalism", "Religon"]
var ResultArray: [NSObject] = []
var infoArray:[AnyObject] = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return genreArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = genreArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestViewController: ResultTableViewController = segue.destinationViewController as! ResultTableViewController
if segue.identifier == "letsGo" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let tappedItem = self.genreArray[indexPath.row]
DestViewController.someString = tappedItem
}
}
}
}
import UIKit
import Firebase
import FirebaseDatabase
class ResultTableViewController: UITableViewController {
let dataBase = FIRDatabase.database()
var SecondResultArray: [FIRDataSnapshot]! = []
var someString: String?{
didSet {
print("I AM A LARGE TEXT")
print(someString)
}
}
override func viewDidLoad() {
let bookRef = dataBase.reference().child("books")
bookRef.queryOrderedByChild("Genre")
.queryEqualToValue(someString)
.observeSingleEventOfType(.Value, withBlock:{ snapshot in
for child in snapshot.children {
self.SecondResultArray.append(child as! FIRDataSnapshot)
//print(self.ResultArray)
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
})
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return SecondResultArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
// Configure the cell...
let bookSnapShot: FIRDataSnapshot! = self.SecondResultArray[indexPath.row]
let book = bookSnapShot.value as! Dictionary<String, String>
let Author = book["Author"] as String!
let Comment = book["Comment"] as String!
let Genre = book["Genre"] as String!
let User = book["User"] as String!
let title = book["title"] as String!
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
cell.textLabel?.text = "Author: " + Author + "\n" + "Comment: " + Comment + "\n" + "Genre: " + Genre + "\n" + "User: " + User + "\n" + "Title: " + title
let photoUrl = book["bookPhoto"], url = NSURL(string:photoUrl!), data = NSData(contentsOfURL: url!)
cell.imageView?.image = UIImage(data: data!)
return cell
}
}
For better context and troubleshooting here is my current code for the tableView which is supposed to display data:
import UIKit
class ResultTableViewController: UITableViewController {
var SecondResultArray: Array<NSObject> = []{
willSet(newVal){
print("The old value was \(SecondResultArray) and the new value is \(newVal)")
}
didSet(oldVal){
print("The old value was \(oldVal) and the new value is \(SecondResultArray)")
self.tableView.reloadData()
}
}
override func viewDidLoad() {
print ("I have this many elements\(SecondResultArray.count)")
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return SecondResultArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
cell.textLabel?.text = SecondResultArray[indexPath.row] as? String
return cell
}
}
Edit:
Here is my first table view controller. I have tried using the completion handler, but I can't call it correctly and I am constricted by the fact that my query happens in the didSelectRowAtIndexPath method. Please help.
import UIKit
import Firebase
import FirebaseDatabase
class GenreTableViewController: UITableViewController {
let dataBase = FIRDatabase.database()
var genreArray = ["Drama","Classic,Comic/Graphic novel","Crime/Detective","Fable,Fairy tale","Fantasy","Fiction narrative", "Fiction in verse","Folklore","Historical fiction","Horror","Humour","Legend","Magical realism","Metafiction","Mystery","Mythology","Mythopoeia","Realistic fiction","Science fiction","Short story","Suspense/Thriller","Tall tale","Western,Biography","Autobiography","Essay","Narrative nonfiction/Personal narrative","Memoir","Speech","Textbook","Reference book","Self-help book","Journalism", "Religon"]
var ResultArray: [NSObject] = []
var infoArray:[AnyObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return genreArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = genreArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
typealias CompletionHandler = (result:NSObject?, error: NSError?) -> Void
func getData(completionHandeler: CompletionHandler){
let bookRef = self.dataBase.reference().child("books")
let GenreSelector = self.genreArray[indexPath.row]
bookRef.queryOrderedByChild("Genre")
.queryEqualToValue(GenreSelector)
.observeSingleEventOfType(.Value, withBlock:{ snapshot in
for child in snapshot.children {
print("Loading group \((child.key!))")
self.ResultArray.append(child as! NSObject)
}
print(self.ResultArray)
self.performSegueWithIdentifier("letsGo", sender: self)
self.tableView.reloadData()
})
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestViewController: ResultTableViewController = segue.destinationViewController as! ResultTableViewController
DestViewController.SecondResultArray = self.ResultArray
}
You can inject the data to the destination viewController in prepareForSegue Method of the first UIViewController and reload your UITableView in viewDidAppear. If you are getting your data asynchronously, have a completionHandler and reload it in the completionHandler. Here is an example.
func fetchDataWithCompletion(response: (NSDictionary?, error:NSError?)-> Void) -> Void {
//make the API call here
}
How about this:
Assume you have an array (myArray) populated from Firebase and stored in the first tableViewController. There's a second tableViewController and a segue connecting them.
We want to be able to tap on an item in the first tableviewController, have the app retrieve detailed data for the item from Firebase (a 'data' node) and display the detailed data in the second tableViewController.
Firebase structure
some_node
child_node_0
data: some detailed data about child_node_0
child_node_1
data: some detailed data about child_node_1
Within the second tableViewContoller:
var passedObject: AnyObject? {
didSet {
self.configView() // Update the view.
}
}
Tapping an item in the first tableView calls the following function
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showListInSecondTable" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let tappedItem = myArray[indexPath.row] as! String
let keyOfTappedItem = tappedItem.firebaseKey //child_node_0 for example
doFirebase(keyOfTappedItem)
}
}
}
and the prepareForSegue then calls the following which loads the data from firebase and when the snapshot returns within the block, it populates the passedObject property in the second tableView
func doFirebase(firebaseKey: String) {
ref = myRootRef.childByAppendingPath("\(firebaseKey)/data")
//if we want the detailed data for child_node_0 this would resolve
// to rootRef/child_node_0/data
ref.observeSingleEventOfType(.Value, { snapshot in
let detailObjectToPass = snapshot.Value["data"] as! NSArray or string etc
let controller = (segue.destinationViewController as! UINavigationController).myViewController as! SecondViewController
controller.passedObject = detailObjectToPass
}
and of course in secondController, setting the passedArray calls didSet and sets up the view, and tells the tableView to reload itself, displaying the passed array.
func configView() {
//set up the view and buttons
self.reloadData()
}
I did this super quick so ignore the typos's. The pattern is correct and satisfies the question. (and eliminates the need for an observer to boot!)
P.S. this is way over coded but I wanted to demonstrate the flow and leveraging the asynchronous call to firebase to load the second tableView when the data was valid within the block.
Try updating your closure to include this:
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
Edit:
On second read, you are already using a completion handler, but I think you didn't see it. Let me correct your code above a bit:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let bookRef = self.dataBase.reference().child("books")
let GenreSelector = self.genreArray[indexPath.row]
bookRef.queryOrderedByChild("Genre")
.queryEqualToValue(GenreSelector)
.observeSingleEventOfType(.Value, withBlock:{ snapshot in
// This here is your completion handler code!
// I assume it is called asynchronously once your DB is done
for child in snapshot.children {
print("Loading group \((child.key!))")
self.ResultArray.append(child as! NSObject)
}
print(self.ResultArray)
self.performSegueWithIdentifier("letsGo", sender: self)
// self.tableView.reloadData() // is this really needed
})
}
}
You defined a closure, but simply didn't call it. I don't see a reason for that anyways, assuming the block gets called once the database gives you your results. Am I missing something?
That's a good start already, but I think you didn't entirely get how to use a completion handler in this regard, but of course I may be wrong.
I built on top of user3861282's answer and created a small demo project at my github.
In short: You can do all inter-table-communication in the prepareForSegue: method of your first table view controller. Configure the second table view controller there (via its vars). Not any closures/completion handlers there yet.
Then in the second view controller's viewWillAppear: method, start the loading (including an animation if you want). I suggest something like NSURLSession that already defines a completion handler. In that you work with your data from remote, stop any loading animations and you're good.
If the completion handler must be defined in the first table view controller, you can even set it as a var in the second table view controller. That way you "hand over" the closure, i.e. "piece of code".
Alternatively, you can start animations and remote request in the first table view controller and then performSegueWithIdentifier once that is done. In your question you wrote that you want to load in the second table view controller, however, if I understood you correctly.
Your code above properly defines a closure that expects a completion handler (which is also a closure and so kind of doubles what you want), but you never actually call it somewhere. Nor do you call the completion handler in the closure. See my demo for how it can work.
The project I wrote illustrates just one way to do it (minus animations, not enough time). It also shows how you can define your own function expecting a completion handler, but as I said, the standard remote connections in the framework provide one anyways.
Based on additional code that was added to the post, the issue is a controller variable going out of scope.
So here's the issue
class MyClass {
func setUpVars {
let x = 1
}
func doStuff {
print(x)
}
}
Create a class and attempt to print the value of x
let aClass = MyClass()
aClass.setUpVars
aClass.doStuff
This will print nothing (conceptually) as once setUpVars ended, the 'x' variable went out of scope.
whereas
class MyClass {
var x: Int
func setUpVars {
x = 1
}
func doStuff {
print(x)
}
}
will print the value of x, 1.
So the real solution is that your viewControllers need to 'stay alive' during the duration of your class (or app).
Here's the pattern. In the MasterViewController
import UIKit
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
then in your MasterViewController viewDidLoad (or wherever), create the detailViewController
override func viewDidLoad() {
super.viewDidLoad()
let controllers = split.viewControllers //this is from a splitViewController
self.detailViewController =
controllers[controllers.count-1].topViewController as? DetailViewController
}
and from there you have it... use prepareForSegue to 'send' the data to the detailViewController
Just wanted to have this posted for future reference.
You can reload the TableView with [tableView reloadData];.
I am trying to implement search in UITableViewController, but not based on filtering existing array, but rather calling my API to search values in remote database.
I thought I implemented everything correctly, because my textDidChange and cellForRowAtIndexPath methods are called in proper time and my data array has all the data I need. BUT - nothing shows up in my TableView! It always says "No Results". I can't find any existing solutions on SO since many classes were deprecated in iOS 8 and I'm struggling with implementing recent ones. Any help appreciated! My code:
class QuestionsController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
var searchController = UISearchController()
var searchText: String?
var areSearchResultsExhausted: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.searchController = UISearchController(searchResultsController: self)
self.searchBar.delegate = self
self.searchController.searchResultsUpdater = self
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit()
self.getQuestionsData(0, searchText: nil)
}
var questionsDataObjects = [Question]()
// MARK: - Get data from API
func getQuestionsData(startFromQuestionId: Int, searchText: String?) {
// Getting data from API stuff like:
let task = session.dataTaskWithRequest(req) { ... }
questionsDataObjects.append(...)
self.tableView.reloadData()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return questionsDataObjects.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("QuestionCell", forIndexPath: indexPath) as! BasicQuestionCell
// Populate the table view cells
let questionCell = questionsDataObjects[indexPath.row]
cell.titleLabel.text = questionCell.content
// Load more results when user scrolled to the end
if (indexPath.row == questionsDataObjects.count-1
&& questionsDataObjects.count > indexPath.row
&& !areSearchResultsExhausted) {
print("qDO.count: \(questionsDataObjects.count) ; indexPath.row: \(indexPath.row)")
self.getQuestionsData(indexPath.row + 1, searchText: self.searchText)
}
return cell
}
// Handle searching
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
self.searchText = searchText
self.areSearchResultsExhausted = false
questionsDataObjects.removeAll()
self.getQuestionsData(0, searchText: searchText)
tableView.reloadData()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
self.searchText = nil
self.areSearchResultsExhausted = false
questionsDataObjects.removeAll()
self.getQuestionsData(0, searchText: searchText)
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
}
Two more important things:
1) updateSearchResultsForSearchController never gets called!
2) I have an error in console that says: Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior (<UISearchController...>) but when I tried to solve this, my search bar stopped working COMPLETELY...
Got it. The problem was: when I dragged the search bar into Storyboard, I selected "Search Bar and Search Display Controller" instead of "Search Bar" itself. Search Display Controller, which is deprecated in iOS8 and later, made the code not work.
So, I had to delete the Search Display Controller from Document Outline, reconnect the outlets to new Search Bar and it worked. Hope it helps somebody.
I am trying to use UISearchController however I confronted with retain issue that I can't solve. MainTableview has two sections.
Section 1
Filtered Data based on some Regex
Section 2
All Data
I added UISearchController to my tableview and attached ResultsTableController as resultsTableController. It works when user search something, ResultsTableController comes forward and because I set tableview delegate to self, selecting item from ResultsTableController calls didSelectRowAtIndexPath in my MainTableViewController. However I have allocation issue if user selects something from resultsTableController.
Following happens for different scenarios
User doesn't search anything, just selects an item from
MainTableview, I see deinit messages
User searches something, cancel the search, select item from
MainTableview, I see deinit messages
User searches something, and selects an item from
ResultsTableController, I don't get deinit in my viewcontrollers
MainTableViewController.swift
var searchController: UISearchController!
// Secondary search results table view.
var resultsTableController: ResultsTableController!
var allCompanies = ["Data1","Data2","Data3"]
override func viewDidLoad() {
super.viewDidLoad()
resultsTableController = ResultsTableController()
// We want to be the delegate for our filtered table so didSelectRowAtIndexPath(_:) is called for both tables.
resultsTableController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsTableController)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
tableView.tableHeaderView = searchController.searchBar
searchController.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
definesPresentationContext = true
}
}
// MARK: UISearchBarDelegate
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
// MARK: UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController) {
// Update the filtered array based on the search text.
let filteredResults = allCompanies.filter({ company in
(company.lowercaseString as NSString).containsString(searchController.searchBar.text.lowercaseString)
})
// Hand over the filtered results to our search results table.
let resultsController = searchController.searchResultsController as! ResultsTableController
resultsController.searchResult = filteredResults
resultsController.tableView.reloadData()
}
// usual tableview methods
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if resultsTableController.searchResult.count > 0 {
selectedCompany = resultsTableController.searchResult[index]
//do something with selected company
navigationController?.popViewControllerAnimated(true)
return
}
//
selectedCompany = allCompanies[index]
navigationController?.popViewControllerAnimated(true)
}
deinit {
println("MainTableView deinit")
}
ResultTableController.swift
class ResultsTableController:UITableViewController {
var searchResult = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResult.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
let index = indexPath.row
cell.textLabel?.font = UIFont(name: "Avenir-Roman", size: 16)
cell.textLabel?.text = searchResult[index].description
return cell
}
deinit {
println("ResultTableController deinit")
}
}
Hey there I ran into the issue today
apparently I need to force the dismiss of the searchController to work around the retain issue
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
searchController?.dismissViewControllerAnimated(false, completion: nil)
}
here is my sample project
https://www.dropbox.com/s/zzs0m4n9maxd2u5/TestSearch.zip?dl=0
The solution does seem to be to call dismissViewControllerAnimated on the UISearchController at some point. Most people probably don't do that since the UISearchController is somewhat of an implementation detail related to your view controller that is hosting the UISearchController.
My solution, which seems to work no matter how you present your search UI (standard present or in a popover) is to call searchController.dismissViewControllerAnimated() from your host's viewDidDisappear, after checking to see if the view controller is no longer being presented. This catches all cases, especially the popover case where the user taps outside the popover to automatically dismiss the UI, or the case where the search UI is disappearing simply because you pushed something else onto the navigation stack. In the latter case, you don't want to dismiss the UISearchController.
override func viewDidDisappear(animated: Bool)
{
super.viewDidDisappear(animated)
if presentingViewController == nil
{
searchController.dismissViewControllerAnimated(false, completion: nil)
}
}