serdar aylanc
serdar aylanc

Reputation: 1347

swift: UISearchcontroller displaying different tableviews before/after search begin

I have a working tableview which I implement UISearchController. Now I want to display 2 different tableviews before and after search action begins, like instagram, twitter. I want to show a different tableview which show recent searches as a list.

My storyboard setup is:

-> tabbar -> navbar -> SearchResultsTableViewController -> navbar -> recentSearchesTableView

I have a searchBar in searchResultsTableView.

class SearchResultsTableViewController: UITableViewController, SearchTableViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()        
        if ((self.searchResults?.count) == 0) {
            performSegueWithIdentifier("ShowSearchView", sender: self)
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ShowSearchView" {
            if let destinationNavigation = segue.destinationViewController as? UINavigationController {
                if destinationNavigation.viewControllers.count > 0 {
                    if let searchVC = destinationNavigation.viewControllers[0] as? SearchTableViewController {
                        searchVC.delegate = self
                    }
                }
            }
        }
    }

Is there way to achive this without using segues?

Upvotes: 1

Views: 59

Answers (2)

rdk
rdk

Reputation: 449

I agree with @Luis Perez, don't use two tableviews.

heres what I would do

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if indexPath.section == 0 {
       return recentSearches.count
    } else {
        if searchController.active && searchController.searchBar.text! != "" {
           return searchResults.count
        }
        return 0
    }
}

Note: you can make the section dividers look however you want since I know you wanted to use 2 tableviews

Upvotes: 1

Luis Perez
Luis Perez

Reputation: 111

You can just change the main tableview content depending of what the user types on the searchbar without sending them to another view, but i dont know if you want to show 2 tables instead of one, for example, if the searcher is empty, the table view can show recent searches, if dont, the table shows content related with the searbar content, can you be more specific ?

Upvotes: 1

Related Questions