Skip to content

Instantly share code, notes, and snippets.

@wethu
Created January 10, 2015 05:21
Show Gist options
  • Select an option

  • Save wethu/f01ccce090b3bb049875 to your computer and use it in GitHub Desktop.

Select an option

Save wethu/f01ccce090b3bb049875 to your computer and use it in GitHub Desktop.
//
// ViewController.m
// comprintapp
//
// Created by Ellis Gray on 8/01/2015.
// Copyright (c) 2015 Ellis Gray. All rights reserved.
//
#import "ViewController.h"
#import "NavController.h"
#import "Client.h"
@interface ViewController () <UISearchBarDelegate, UISearchResultsUpdating, UITableViewDataSource, UISearchControllerDelegate>
// Bar button items
@property (nonatomic) UIBarButtonItem *jobButton;
@property (nonatomic) UIBarButtonItem *clientButton;
// Table view and delegate objects
@property (nonatomic) UITableView *tableView;
// Search
@property (nonatomic) UISearchController *searchController;
@property (nonatomic) NSMutableArray *searchResults;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Configure view
self.view.backgroundColor = [UIColor colorWithWhite:.5f alpha:1];
// Init properties
_searchResults = [NSMutableArray arrayWithCapacity:[self.clients count]];
_clients = [Client allPersistedClients];
// Add barbutton items and tableview
self.navigationItem.leftBarButtonItems = @[self.jobButton, self.clientButton];
[self.view addSubview:self.tableView];
}
#pragma mark - TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.searchController.active ? self.searchResults.count : self.clients.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
// Configure client for where ever it comes from
Client *client = self.searchController.active ? [self.searchResults objectAtIndex:indexPath.row] : [self.clients objectAtIndex:indexPath.row];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellID"];
cell.textLabel.text = client.businessName;
cell.detailTextLabel.text = client.contactName;
return cell;
}
#pragma mark - UI Bar Button Items
- (UIBarButtonItem *)jobButton {
if (!_jobButton) {
_jobButton = [[UIBarButtonItem alloc] initWithCustomView:[CPUIHelper menuButtonWithTarget:self action:@selector(jobsTapped:) title:@"Jobs"]];
}
return _jobButton;
}
- (UIBarButtonItem *)clientButton {
if (!_clientButton) {
_clientButton = [[UIBarButtonItem alloc] initWithCustomView:[CPUIHelper menuButtonWithTarget:self action:@selector(clientsTapped:) title:@"Clients"]];
}
return _clientButton;
}
#pragma mark - UI Elements
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor colorWithWhite:.3 alpha:1];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableHeaderView = self.searchController.searchBar;
CGPoint offset = CGPointMake(0, self.searchController.searchBar.frame.size.height);
_tableView.contentOffset = offset;
}
return _tableView;
}
- (UISearchController *)searchController {
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.dimsBackgroundDuringPresentation = YES;
_searchController.searchResultsUpdater = self;
_searchController.delegate = self;
_searchController.searchBar.frame = CGRectMake(0,0,320,44);
_searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
_searchController.searchBar.layer.backgroundColor = [UIColor whiteColor].CGColor;
self.tableView.tableHeaderView = _searchController.searchBar;
}
return _searchController;
}
#pragma mark - Handle Update
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];
[self updateFilteredContentForClient:searchString];
}
- (void)updateFilteredContentForClient:(NSString *)searchString {
if (searchString != nil || searchString.length > 0) {
self.searchResults = [NSMutableArray arrayWithArray:[Client filterClientsForTerm:searchString]];
} else {
[self.searchResults removeAllObjects];
}
[self.tableView reloadData];
}
#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"indexPath: %@", indexPath);
}
#pragma mark - User Events
- (void)clientsTapped:(UIButton *)sender {
RKManagedObjectRequestOperation *operation = [Client clientRequestOperation];
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (mappingResult)
[self.tableView reloadData];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
[NSException raise:NSGenericException format:@"Error fetching clients operation: %@ - %@", operation, error];
}];
[operation start];
}
- (void)jobsTapped:(UIButton *)sender {
NSLog(@"Jobs Tapped");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment