我刚开始学习iOS编程。我正在搜索栏上工作。我关注了appcoda新教程http://www.appcoda.com/search-bar-tutorial-ios7/。我在某个方面陷入了困境。我试图根据我的搜索文本显示过滤结果。似乎问题是在过滤后重新加载数据。我将搜索栏和搜索显示控制器拖到故事板上的View Controller中。这是我输入文本时遇到的错误。你能帮忙吗?谢谢你,感谢抱歉。
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
答案 0 :(得分:1)
您正在查看的示例包含具有属性名称的Recipe类并在属性名称上进行搜索。在您的数组中,您没有任何属性名称数组。在NSPredicate中,您可以使用SELF而不是名称。
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}
我还建议您浏览此链接,这将有所帮助https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html
答案 1 :(得分:0)
检查xib文件以查找与IBOutlet相关的任何视图。此错误的可能性之一是从.h文件中删除IBOutlet,但它仍然连接到xib文件中的某个视图。这只是一种可能性。