diff --git a/MonsterCards/Base.lproj/Main.storyboard b/MonsterCards/Base.lproj/Main.storyboard
index 026f497..b5e01e2 100644
--- a/MonsterCards/Base.lproj/Main.storyboard
+++ b/MonsterCards/Base.lproj/Main.storyboard
@@ -17,7 +17,7 @@
-
+
@@ -25,62 +25,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -93,13 +38,10 @@
-
-
-
-
+
@@ -112,13 +54,10 @@
-
-
-
-
+
@@ -131,50 +70,92 @@
-
-
-
-
+
-
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
diff --git a/MonsterCards/Views/MonsterViewController.m b/MonsterCards/Views/MonsterViewController.m
index 506395e..55f341b 100644
--- a/MonsterCards/Views/MonsterViewController.m
+++ b/MonsterCards/Views/MonsterViewController.m
@@ -16,18 +16,11 @@
- (void)viewDidLoad {
[super viewDidLoad];
- self.monsterName.text = _monster.name;
- // Do any additional setup after loading the view.
+ if (self.monsterName != nil) {
+ self.monsterName.text = self.monster.name;
+ } else if (self.navigationItem != nil && self.navigationItem.title != nil) {
+ self.navigationItem.title = self.monster.name;
+ }
}
-/*
-#pragma mark - Navigation
-
-// In a storyboard-based application, you will often want to do a little preparation before navigation
-- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
-}
-*/
-
@end
diff --git a/MonsterCards/Views/SearchViewController.h b/MonsterCards/Views/SearchViewController.h
index b08c2dc..a3fd563 100644
--- a/MonsterCards/Views/SearchViewController.h
+++ b/MonsterCards/Views/SearchViewController.h
@@ -10,7 +10,7 @@
NS_ASSUME_NONNULL_BEGIN
-@interface SearchViewController : UIViewController
+@interface SearchViewController : UITableViewController
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *searchResults;
diff --git a/MonsterCards/Views/SearchViewController.m b/MonsterCards/Views/SearchViewController.m
index 4f75109..f434423 100644
--- a/MonsterCards/Views/SearchViewController.m
+++ b/MonsterCards/Views/SearchViewController.m
@@ -12,23 +12,19 @@
@interface SearchViewController ()
+@property NSArray* allMonsters;
+@property NSArray* foundMonsters;
+
@end
-@implementation SearchViewController {
- NSMutableArray *_monsters;
-}
+@implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
- _monsters = [[NSMutableArray alloc] init];
- Monster *monster;
- monster = [[Monster alloc] init];
- monster.name = @"Pixie";
- [_monsters addObject:monster];
- monster = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
- [_monsters addObject:monster];
-
- // Do any additional setup after loading the view.
+ Monster *pixie = [[Monster alloc] initWithJSONString:@"{\"name\":\"Pixie\"}"];
+ Monster *acolyte = [[Monster alloc] initWithJSONString:@"{\"name\":\"Acolyte\"}"];
+ self.allMonsters = [NSArray arrayWithObjects:acolyte, pixie, nil];
+ self.foundMonsters= self.allMonsters;
}
#pragma mark - Navigation
@@ -40,37 +36,56 @@
if ([@"ShowMonsterDetail" isEqualToString:segue.identifier]) {
NSIndexPath *indexPath = [self.searchResults indexPathForSelectedRow];
MonsterViewController *vc = (MonsterViewController*)segue.destinationViewController;
- vc.monster = [_monsters objectAtIndex:indexPath.row];
+ vc.monster = [self.foundMonsters objectAtIndex:indexPath.row];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
- return [_monsters count];
+ return [self.foundMonsters count];
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
- static NSString *simpleTableIdentifier = @"SimpleTableItem";
+ static NSString *simpleTableIdentifier = @"MonsterCell";
- UITableViewCell *cell = [_searchResults dequeueReusableCellWithIdentifier:simpleTableIdentifier];
+ UITableViewCell *cell = [self.searchResults dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
- cell.textLabel.text = ((Monster*)[_monsters objectAtIndex:indexPath.row]).name;
+ Monster *monster = (Monster*)[self.foundMonsters objectAtIndex:indexPath.row];
+
+ cell.textLabel.text = monster.name;
return cell;
}
-#pragma mark - UITableViewDelegate
+#pragma mark - UISearchBarDelegate
-// Tap on table Row
-- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath {
+- (void)searchBar:(UISearchBar *)searchBar
+ textDidChange:(NSString *)searchText {
- [self performSegueWithIdentifier:@"ShowMonsterDetail" sender:self];
+ if (searchText != nil && ![@"" isEqualToString:searchText]) {
+ NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id item, NSDictionary *bindings) {
+ if (![item isKindOfClass:[Monster class]]) {
+ return false;
+ }
+ Monster *monster = (Monster*)item;
+
+ if ([monster.name localizedCaseInsensitiveContainsString:searchText]) {
+ return true;
+ }
+
+ return false;
+ }];
+ self.foundMonsters = [self.allMonsters filteredArrayUsingPredicate:predicate];
+ } else {
+ self.foundMonsters = self.allMonsters;
+ }
+
+ [self.tableView reloadData];
}
-
@end