Select, Search, Delete in UITableView with powerful ViewController

The Requirement:

Very often, you have a need to:

  • display some records to the user from originating screen (view controller)
  • have user select one / many of them and return to your original screen
  • have user search through them by some field
  • have user delete some of them

This being a recurring pattern, sometimes it results in lot of duplication of code within your project.

The Solution:

Here is a simple yet powerful solution to this problem: TableSearchViewController. To be precise, this is a simple UIViewController that hosts a UITableView, and displays your supplied objects (Array) in a neat, customized way. You can choose which fields to display in rows, which fields to make search with, and how to present the data in particular format.

You can even be selective in UITableView appearance – you can select cell colors, text colors, section header colors, checkbox images and delete button images.

  • If you look carefully in the UI, it shows checkbox-flavored UITableView rows. Each time you discard the screen using Select, it gives you selected objects in an array (of original object types) in a block, defined using originating view controller’s code.
  • You can select entire section using section checkbox. Not only that, you also get one extra checkbox in the footer, which returns a bool parameter into the selection block each time you discard using “Select”.
  • You can search records using strings, numbers, or any field (property) provided your object is KVC compliant. This is highly useful when you display an array of NSManagedObjects inside TableSearchViewController.

 


















The Code:

And here is the code snippet to achieve this appearance:

TableSearchViewController * tableSearchViewController = [[TableSearchViewController alloc] initWithNibName:@"TableSearchViewController" bundle:[NSBundle mainBundle]];

CustomObject * firstObj = [[CustomObject alloc] initWithString:@"First" andNumber:@(1)];
CustomObject * secondObj = [[CustomObject alloc] initWithString:@"Second" andNumber:@(2)];
CustomObject * thirdObj = [[CustomObject alloc] initWithString:@"Third" andNumber:@(3)];
CustomObject * forthObj = [[CustomObject alloc] initWithString:@"Four" andNumber:@(4)];
CustomObject * fifthObj = [[CustomObject alloc] initWithString:@"Five" andNumber:@(5)];

NSArray * firstSectionArray = @[firstObj, secondObj, thirdObj];
NSArray * secondSectionArray = @[forthObj, fifthObj, firstObj];

tableSearchViewController.resultsArray = @[@{@"First Section" : firstSectionArray}, @{@"Second Section" : secondSectionArray}];
tableSearchViewController.tableViewStyle = UITableViewStyleGrouped;
tableSearchViewController.allowSearch = YES;
tableSearchViewController.searchKeys = @[@"stringProperty"];
tableSearchViewController.textLabelKeys = @[@"stringProperty"];
tableSearchViewController.subTitleKeys = @[@"numberProperty"];
tableSearchViewController.textLableFormats = @[@"%@"];
tableSearchViewController.subTitleFormats = @[@"%@"]; 
tableSearchViewController.selectionDoneButtonTitle = @"Select";
tableSearchViewController.dismissButtonTitle = @"Cancel";
tableSearchViewController.accessoryAction = ACCESSORY_ACTION_CHECK;
tableSearchViewController.allowSelectionCheckImage = YES;
tableSearchViewController.allowSelectAllCheckBox = YES;
tableSearchViewController.cellColorStyle = CELL_COLOR_STYLE_ALTERNATE_DOUBLE;
tableSearchViewController.selectionDoneBlock = ^(NSArray* _Nullable  selectedKVCObjects, BOOL bExtraFlag)

{
        NSLog(@"%@", selectedKVCObjects);
        _valueLabel.text = [NSString stringWithFormat:@"%@", [selectedKVCObjects componentsJoinedByString:@","]];
};    

UINavigationController * navCtrl = [[UINavigationController alloc] initWithRootViewController:tableSearchViewController];
[self presentViewController:navCtrl animated:YES completion:nil];

CustomObject in the above is an NSObject derived KVC compliant object, included in the Github project.

The Swift version is under progress, and should soon be available.

Easy, isn’t it? So do feel free to download, leverage, and improve!

Tagged with:

Leave a Reply

Your email address will not be published. Required fields are marked *

*