在搜索栏中键入文本时出错 iOS8

Getting error while typing a text in search bar iOS8

我刚开始学习 iOS 编程。我正在研究搜索栏。我遵循了 appcoda 新教程 http://www.appcoda.com/search-bar-tutorial-ios7/ 。我被困在某个时刻。我正在尝试根据我的搜索文本显示过滤后的结果。看来问题是过滤后重新加载数据。我将搜索栏和搜索显示控制器拖到故事板上的视图控制器中。这是我在键入文本时遇到的错误。你能帮忙吗?谢谢你,很抱歉这么长 post.

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

这是我的 .h 和 .m 文件。

#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

检查您的 xib 文件以查找与您的 IBOutlet 相关联的任何视图。此错误的一种可能性是当您从 .h 文件中删除 IBOutlet 时,但它仍然连接到 xib 文件中的某个视图。只是一种可能。

您正在查看的示例中的食谱 class 名称为 属性 并且正在搜索 属性 名称。在您的数组中,您没有任何 属性 名称数组。在 NSPredicate 中,您可以使用 SELF 而不是使用名称。

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

我还建议您仔细阅读此 link 这将很有帮助 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html