Makes the search bar redo the search when the view is reloaded.
This commit is contained in:
@@ -29,8 +29,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (void)viewWillAppear:(BOOL)animated {
|
- (void)viewWillAppear:(BOOL)animated {
|
||||||
|
NSString *searchText = nil;
|
||||||
|
if (self.searchBar != nil) {
|
||||||
|
searchText = self.searchBar.text;
|
||||||
|
}
|
||||||
self.allMonsters = [_context executeFetchRequest:[Monster fetchRequest] error:nil];
|
self.allMonsters = [_context executeFetchRequest:[Monster fetchRequest] error:nil];
|
||||||
self.foundMonsters= self.allMonsters;
|
self.foundMonsters = [self filterAllMonstersWithText:searchText];
|
||||||
|
[self.tableView reloadData];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Navigation
|
#pragma mark - Navigation
|
||||||
@@ -69,9 +74,7 @@
|
|||||||
|
|
||||||
#pragma mark - UISearchBarDelegate
|
#pragma mark - UISearchBarDelegate
|
||||||
|
|
||||||
- (void)searchBar:(UISearchBar *)searchBar
|
- (NSArray*)filterAllMonstersWithText:(NSString *)searchText {
|
||||||
textDidChange:(NSString *)searchText {
|
|
||||||
|
|
||||||
if (searchText != nil && ![@"" isEqualToString:searchText]) {
|
if (searchText != nil && ![@"" isEqualToString:searchText]) {
|
||||||
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id item, NSDictionary *bindings) {
|
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id item, NSDictionary *bindings) {
|
||||||
if (![item isKindOfClass:[Monster class]]) {
|
if (![item isKindOfClass:[Monster class]]) {
|
||||||
@@ -85,10 +88,16 @@
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}];
|
}];
|
||||||
self.foundMonsters = [self.allMonsters filteredArrayUsingPredicate:predicate];
|
return [self.allMonsters filteredArrayUsingPredicate:predicate];
|
||||||
} else {
|
} else {
|
||||||
self.foundMonsters = self.allMonsters;
|
return self.allMonsters;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)searchBar:(UISearchBar *)searchBar
|
||||||
|
textDidChange:(NSString *)searchText {
|
||||||
|
|
||||||
|
self.foundMonsters = [self filterAllMonstersWithText:searchText];
|
||||||
|
|
||||||
[self.tableView reloadData];
|
[self.tableView reloadData];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user