Latest 0.1.1
Homepage https://github.com/timgcarlson/TCTableViewSearchController
License MIT
Platforms ios 8.0, requires ARC
Frameworks UIKit
Authors

[![CI Status](http://img.shields.io/travis/Tim G Carlson/TCTableViewSearchController.svg?style=flat)](https://travis-ci.org/Tim G Carlson/TCTableViewSearchController)
Version
License
Platform

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

TCTableViewSearchController is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "TCTableViewSearchController"

Create a subclass of TCTableViewSearchController (or present it as you would any other view controller) and go about creating your table view like you normally would had you subclassed UITableViewController. Declare that your class adopts the protocol in the class definition (TCTableViewSearchControllerDelegate). Your table must be generated from objects that have properties. Currently you cannot have a table where the data source is an array of strings.

Then perform the following steps:

1) Accessing the correct item or item count

In any UITableViewDataSource or UITableViewDelegate method that acts on an object at an index path or needs an item count, you will need to determine whether to use you main data source or the filteredResults data source. You can do this by checking whether the search bar is currently active.


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (!self.searchController.active) {
    // return the count of your main data source
  } else {
    // return the count of the filteredResults property of TCTableViewSearchController
  }
}

2) Implement the required delegate methods of TCTableViewSearchControllerDelegate.

To keep things simple, there is only one method required to search object data in your table. You will need to provide the objects to search, the properties (in the form of strings in an array) that you want to be searched for each object, and the object that contains these properties. The example object is needed to determine the type of the property, which it has to be a NSString or NSNumber.


- (void)updateSearchResultsForSearchingTableViewController:(TCTableViewSearchController *)searchingTableViewController withCompletion:(TCSearchBlock)completion {
  // Provide the properties of the objects in the table that you wish to search. Currently only supports NSStrings and NSNumbers.
  NSArray *propertiesArray = [NSArray arrayWithObjects:@"name", @"breed", @"ownerName", @"birthYear", nil]; // These are the properties of the TCDog object represented in string form
  completion(self.dogArray, propertiesArray, [TCDog dog]);  // The data source, the properties to search, and an example object that will be searched.
}

3) (optional) Implement scope bar.

If a scope bar is desired, then you will likely be searching two or more properties of the object. So if you are searching four properties in the required delegate method updateSearchResultsForSearchingTableViewController:, then you will need to create five titles for the scope bar where the first title in the scope title array is the "Search All" scope. Each additional scope should correspond with the order of properties that are being searched (an exception will be thrown otherwise).


- (NSArray *)scopeBarTitles {
  return @[@"All", @"Name", @"Breed", @"Owner", @"Birth Year"];
}

Implementing this method will create the scope bar for you, no other action is needed.

Author

Tim G Carlson, [email protected]

License

TCTableViewSearchController is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "TCTableViewSearchController",
    "version": "0.1.1",
    "summary": "A subclass of UITableViewController that handles basic search bar functionality to make searching a table a painless process.",
    "description": "Sometimes all you want is a table view that will handle most of the basic searching for you. That's where TCTableViewSearchController comes in. It handles most of the UISearchController requirements for you. All that you will need to do is implement one method that tells it which properties to search in the objects populating your table view. Currently only supports searching NSStrings and NSNumbers. Also has basic (optional) scope bar support.nn**This includes a demo project to show you the ropes. See the README file for usage instructions.**",
    "homepage": "https://github.com/timgcarlson/TCTableViewSearchController",
    "license": "MIT",
    "authors": {
        "Tim G Carlson": "[email protected]"
    },
    "source": {
        "git": "https://github.com/timgcarlson/TCTableViewSearchController.git",
        "tag": "0.1.1"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes",
    "resource_bundles": {
        "TCTableViewSearchController": [
            "Pod/Assets/*.png"
        ]
    },
    "public_header_files": "Pod/Classes/TCTableViewSearchController.h",
    "frameworks": "UIKit"
}

Pin It on Pinterest

Share This