2

I have a searchBar in a tableview, searching for results in a .csv file (coredata).
The list is huge so the user has to scroll up many times to reach the search bar after the first search OR select the "A" letter in the Indexbar.
Is there a way to add a button in the NavigationBar to show the searchBar when the user wants to get back to the beginning of the list? Thanks in advance.

    searchController = UISearchController(searchResultsController: nil)
    tableView.tableHeaderView = searchController.searchBar
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false


   override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sectionTitles[section]
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        //assume a single section after a search
        return (searchController.active) ? 1 : sectionTitles.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if searchController.active {
            return searchResults.count
        } else {
            // Return the number of rows in the section.
            let wordKey = sectionTitles[section]
            if let items = cockpitDict[wordKey] {
                return items.count
            }

            return 0
        }
    }

4 Answers4

1

Yes, you can add a UIBarButtonItem to your navigation bar where the action you do will scroll the table back to the top.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • @ Michael Dautermann I edited my question with some code. It is a different from the code link you provided, any help with mine? Thanks –  Jan 20 '16 at 13:06
1

I don't know how to send the user direct to a search bar, but there are always other ways to do things, what you can do is: Reload the storyboard and then it will show again the search bar in the initial states. Take a look on this interesting post: How do I perform an auto-segue in Xcode 6 using Swift? maybe you can go to another VC and return immediately with an simple animation so the user will not notice the tricker. :)

Community
  • 1
  • 1
  • I did not think about this it is a good idea.... I will take a look, thank you Eddie..... –  Feb 02 '16 at 20:15
  • Works great, and I could set an image to make something different. thanks –  Feb 03 '16 at 06:00
0

Just replace the code from tableView.tableHeaderView = searchController.searchBar to navigationItem.titleView = searchController.searchBar which shows the searchBar on the NavigationBar.

But when you select the searchBar then it goes upward and might be not visible on the screen, so you can take the UISearchBar instead of UISearchController. For more information please look into these thread.

Community
  • 1
  • 1
Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
  • yes the search bar disappears, I am trying to get it back but unable. what do you mean change to UISearchBar.... can you give me the line? thanks :) –  Jan 29 '16 at 15:59
  • Did you check the link which I had shared ? – Ramkrishna Sharma Feb 01 '16 at 06:19
  • @RamkrishnaSharma yes I did but it is not Swift. Do you know how can I get back the searchBar using your code above? – Manolo Feb 02 '16 at 07:33
  • @Manolo : It is swift please check again. And regarding get back the searchBar so are you implementing the UISearchBar or UISearchController please let me know so that I can help you on this. – Ramkrishna Sharma Feb 02 '16 at 09:13
  • @Manolo can you go to this chat? http://chat.stackoverflow.com/rooms/102335/searchbarproblem –  Feb 02 '16 at 09:37
  • @RamkrishnaSharma can you please join me on this chat? chat.stackoverflow.com/rooms/102335/searchbarproblem –  Feb 02 '16 at 12:29
0

This is not exactly an answer to your question, but still a solution to your problem of quickly returning to the top of your table view. You could overwrite the table view's scrollsToTop: -property to return YES. By doing so, you will enable the user to jump to the top of the table by simply tapping the status bar. This is standard behavior in many stock apps, such as Contacts, Mail, Safari, and Photos.

Beware that only one scroll view / table view / collection view on the screen may return YES in order to achieve this behavior. In the case of multiple scroll views, you can alternatively implement the UIScrollViewDelegate -protocol's

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView

callback and dynamically check, if the given scroll view should respond to the tap.

Although this is the Objective-C -way, I guess you should be able to transfer the concept over to Swift.

This approach has two advantages:

  1. You will not clutter your navigation bar. Apples iOS Human Interface Guidelines explicitly suggest to

    Avoid crowding a navigation bar with additional controls, even if it looks like there’s enough space. In general, a navigation bar should contain no more than the view’s current title, the back button, and one control that manages the view’s contents.

  2. You will be consistent with the OS' standard behavior.

Community
  • 1
  • 1
Manfred Urban
  • 428
  • 5
  • 13