Ahmet Tanakol
Ahmet Tanakol

Reputation: 899

Getting error while typing a text in search bar iOS8

I just started to learn iOS programming. I was working on search bar. I followed appcoda new tutorial http://www.appcoda.com/search-bar-tutorial-ios7/ . I'm stuck at some point. I'm trying to display filtered results according to my search text. It seems the problem is reloading data after filtering. I dragged Search Bar and Search Display Controller into my View Controller on storyboard. This is the error I get when I type a text. Can you please help? Thank you and sorry for the long post.

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x108655078> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'

Here are my .h and .m files.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UISearchDisplayDelegate,UISearchBarDelegate,UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) IBOutlet UITableView *tableView;


@end

///

#import "ViewController.h"
#import "RecipeDetailViewController.h"


@interface ViewController ()

@end

@implementation ViewController {

NSArray *recipes;
NSArray *searchResults;
NSString *name;
}

@synthesize tableView;


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];

} else {
    return [recipes count];
 }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {
    name = [searchResults objectAtIndex:indexPath.row];

} else {
    name = [recipes objectAtIndex:indexPath.row];
}

cell.textLabel.text = name;
return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    RecipeDetailViewController *destViewController = segue.destinationViewController;
    destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
}
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}

-(bool)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
                           scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                  objectAtIndex:[self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]];

return YES;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 71;
}

@end

Upvotes: 0

Views: 159

Answers (2)

Shashi3456643
Shashi3456643

Reputation: 2041

The example you are looking at it has Recipe class with property name and doing search on property name. Here in your array you don't have any property name array. In NSPredicate instead of using name you can use SELF.

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}

I would also recommend go through this link which will be helpful https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html

Upvotes: 1

Ans
Ans

Reputation: 3571

Check your xib file for any view connected with your IBOutlet. One of the possibility of this error is when you delete an IBOutlet from .h file, but it is still connected to some view in xib file. Its just a possibility.

Upvotes: 0

Related Questions