Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

eberhapa
eberhapa
51,495 Points

How to check if searchcontroller isActive in external dataSource file?

Hi folks, hi Pasan Premaratne

I like the way you split the datasource into an extra file in the contactsApp course. I'm trying to implement a search possibility but im stuck with getting access to the searchController.isActive function in the datasource file. Whats the correct way to handle this issue?

3 Answers

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

eberhapa

Apologies on the delay, I've been out of the office. Are you trying to search through the contacts? If you're using the built in search, you can conform to UISearchResultsUpdating to find out what is being entered

eberhapa
eberhapa
51,495 Points

Hi Pasan, No problem :) The problem is that i have the following code in the ContactListController:

       lazy var searchResultController: UISearchController = {
        let src = UISearchController(searchResultsController: nil)
        src.delegate = self
        src.searchResultsUpdater = self
        src.dimsBackgroundDuringPresentation = false
        src.hidesNavigationBarDuringPresentation = false
        src.definesPresentationContext = true
        src.searchBar.sizeToFit()
        return src
    }()   

But i want to use the isActive method of the searchResultsController in the ContractsDataSource like:

       func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        if searchResultController.isActive {
            return nil
        }
        return sectionTitles
    } 

But the searchResultController is not available at the point where we pass the data to the dataSource, so I'm not able to pass the searchResultsController. If i use the massiveViewController approach everything is working fine. But thats really awful :D

Or should i pass the searchController in the updateSearchResults of the UISearchResultsUpdating Protocol? But that would reassign the searchController every time i type something.

All the best, Patrick

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

You could pass the search controller to the data source. Create a private stored property in the data source and define an initializer parameter.

Or you could define an entirely different table view controller to use in displaying search results. In this line of code:

let src = UISearchController(searchResultsController: nil)

By specifying nil you're saying that the search controller should use the table view it is part of to display results. You could provide a different table view controller as the argument there with a different data source

eberhapa
eberhapa
51,495 Points

It's working, thanks. I had to set dataSource variable as an optional ContactDataSource and assign the DataSource in viewDidLoad. Hope thats the correct way to do it.