Skip to content

Instantly share code, notes, and snippets.

@anthonycvella
Created January 4, 2013 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonycvella/4449753 to your computer and use it in GitHub Desktop.
Save anthonycvella/4449753 to your computer and use it in GitHub Desktop.
//
// TradeSelectNewTableViewController.m
// WarZ Companion
//
// Created by Anthony on 1/3/13.
// Copyright (c) 2013 Anthony. All rights reserved.
//
#import "ItemDatabase.h"
#import "TradeSelectNewTableViewController.h"
@interface TradeSelectNewTableViewController ()
@end
@implementation TradeSelectNewTableViewController
@synthesize itemDatabaseArray, filteredItemDatabaseArray, itemSearchBar, checkedIndexPath;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
itemDatabaseArray = [NSArray arrayWithObjects:
[ItemDatabase itemOfCategory:@"weapons" name:@"Bat"],
[ItemDatabase itemOfCategory:@"weapons" name:@"Flashlight"], nil];
self.filteredItemDatabaseArray = [NSMutableArray arrayWithCapacity:[itemDatabaseArray count]];
[[self tableView] reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredItemDatabaseArray count];
} else {
return [itemDatabaseArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
//Create new Item Database Object
ItemDatabase *item = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
item = [filteredItemDatabaseArray objectAtIndex:indexPath.row];
} else {
item = [itemDatabaseArray objectAtIndex:indexPath.row];
}
//Configure the cell
[[cell textLabel] setText:[item name]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Uncheck the previous checked row
if (self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
// Check the new row
if ([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
} else {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
//[self performSegueWithIdentifier:@"itemDetail" sender:tableView];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier]isEqualToString:@"itemDetail"]) {
TradeNewTableViewController *tradeNewTableViewController = [segue destinationViewController];
if (sender == self.searchDisplayController.searchResultsTableView) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *selectedItem = [[itemDatabaseArray objectAtIndex:[indexPath row]] name];
tradeNewTableViewController.selectedItem = selectedItem;
} else {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *selectedItem = [[itemDatabaseArray objectAtIndex:[indexPath row]] name];
tradeNewTableViewController.selectedItem = selectedItem;
}
}
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredItemDatabaseArray removeAllObjects];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText];
filteredItemDatabaseArray = [NSMutableArray arrayWithArray:[itemDatabaseArray filteredArrayUsingPredicate:predicate]];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
- (IBAction)btnNext:(id)sender {
[self performSegueWithIdentifier:@"itemDetail" sender:tableView];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment