15

I'm wondering what solution is the best for the pull down action: Pull-To-Refresh vs. Pull-To-Search? What if I want to have both (search bar and refresh) like in the Mail app? Don't know what the approval in the app store does look like ...

How would you implement that with a UITableViewController?

Acey
  • 8,048
  • 4
  • 30
  • 46
grabner
  • 1,219
  • 3
  • 13
  • 23

2 Answers2

46

You can have both Pull-To-Refresh and Pull-To-Search in a UITableViewController quite easily.

Here's Pull to Refresh:

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
[self.tableView addSubview:refresh];
self.refreshControl = refresh;

To know when it has been 'pulled', add a target to the control:

[self.refreshControl addTarget:self action:@selector(refreshContent:) forControlEvents:UIControlEventValueChanged];

Here's "Pull-To-Search":

UISearchBar *searchBar = [[UISearchBar alloc] init];
UISearchDisplayController *searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
self.tableView.tableHeaderView = searchBar;
self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(searchBar.frame));

As you can see, Pull-To-Search is really just adding the searchBar as the tableHeaderView and offsetting the tableView a bit initially so the search bar isn't shown initially. That doesn't interfere with Pull-To-Refresh at all!

Acey
  • 8,048
  • 4
  • 30
  • 46
  • 10
    UISearchDisplayController is deprecated in iOS 8, use UISearchController instead. – Siegfoult Oct 22 '14 at 21:37
  • 2
    As Siegfoult said, the UISearchDisplayController is deprecated in iOS 8. I found a official sample from Apple how implement search the "new" way: https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html – Primoz990 Sep 18 '15 at 12:09
  • 1
    This did the trick! Why do we need to add the refreshControl as a subview to the tableView for it to work? – thegreenfrog Jun 19 '16 at 21:55
  • Technically pull to refresh can work for any UIScrollViews, but only UITableView has the refreshControl property which isn't even required to set if I remember correctly. UICollectionView doesn't have it but this control works fairly well for it. Basically there is some magic in UIScrollView that looks for this sort of control in its subviews and makes things work. Sorry that is vague but like I said, it's a bit magic and blackbox so I don't know exactly. – Acey Jun 19 '16 at 23:07
2

Here is how that would work for Swift 2 using UISearchController:

class YourTableViewController: UITableViewController, UISearchResultsUpdating {

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize the refresh control
    self.refreshControl!.addTarget(self, action: #selector(YourTableViewController.refresh(_:)), forControlEvents: UIControlEvents.ValueChanged)

    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchController.searchBar
    self.tableView.contentOffset = CGPointMake(0, CGRectGetHeight(searchController.searchBar.frame));

}

".refresh(_:)" refers to

func refresh(sender:AnyObject)
{
//Load Data

}

Make sure Refreshing is enabled.

https://i.stack.imgur.com/e1K34.png

The first answer to this question helped: How to implement UISearchController in UITableView - SWIFT

I also found this tutorial helpful: http://www.appcoda.com/custom-search-bar-tutorial/

Community
  • 1
  • 1
thejuki
  • 1,351
  • 2
  • 13
  • 16