您好我的应用程序中有一个TableView,我在其中显示了一些内容,我想在此TableView中搜索我在搜索栏中写入的文本,因此我按照tutorial进行了操作。
在我的应用程序中我遇到了问题,当我运行应用程序并尝试搜索崩溃的字母时,在xCode中我看到了这些信息:Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x9e45990> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
。我检查了我的故事板,看看我是否有问题,但一切都好。所以我想问题是代码中的问题,我会在这里发布我的代码,希望你能帮我解决问题。
#import "TableWasteViewController.h"
#import "WasteXmlParser.h"
#import "WasteDetailViewController.h"
@interface TableWasteViewController ()
@property(nonatomic,strong)NSArray *arrayWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWaste;
@property(nonatomic,strong)NSMutableArray *typeOfBin;
@property(nonatomic,strong)NSMutableArray *indexWastes;
@property(nonatomic,strong)NSMutableArray *typeOfWasteBackup;
@end
@implementation TableWasteViewController
@synthesize searchBar;
@synthesize filteredWasteArray;
@synthesize typeOfWaste;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
WasteXmlParser *parser = [[WasteXmlParser alloc]init];
[parser parseWasteXml];
self.arrayWastes = [[NSArray alloc]init];
self.arrayWastes = [parser.arrayWastes mutableCopy];
self.indexWastes = [[NSMutableArray alloc]init];
typeOfWaste = [[NSMutableArray alloc]init];
self.typeOfBin = [[NSMutableArray alloc]init];
for (int i = 0; i < [self.arrayWastes count]; i++) {
[typeOfWaste addObject:[self.arrayWastes[i] objectForKey:@"type"]];
}
self.filteredWasteArray = [NSMutableArray arrayWithCapacity:[self.typeOfWaste count]];
self.typeOfWasteBackup = [self.typeOfWaste mutableCopy];
}
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
[self.filteredWasteArray removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];
filteredWasteArray = [NSMutableArray arrayWithArray:[typeOfWaste filteredArrayUsingPredicate:predicate]];
}
#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//return self.arrayWastes.count;
if (self.tableWaste == self.searchDisplayController.searchResultsTableView) {
return [filteredWasteArray count];
} else {
return [self.arrayWastes count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *myFont = [UIFont fontWithName:@"Arial" size:14.0];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.font = myFont;
cell.textLabel.numberOfLines = 2;
//cell.textLabel.text = [self.arrayWastes[indexPath.row]objectForKey:@"type"];
if (self.tableWaste == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [self.filteredWasteArray[indexPath.row]objectForKey:@"type"];
} else {
cell.textLabel.text = [self.arrayWastes[indexPath.row]objectForKey:@"type"];
}
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableWaste indexPathForSelectedRow];
WasteDetailViewController *vc = segue.destinationViewController;
vc.typeOfWaste = [self.arrayWastes[indexPath.row]objectForKey:@"type"];
vc.typeOfBin = [self.arrayWastes[indexPath.row]objectForKey:@"place"];
vc.urlPic = [self.arrayWastes[indexPath.row]objectForKey:@"imgUrl"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)backToHome:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
谢谢!
更新 我在这里发布我的故事板的屏幕,你可以在其中看到tableview控制器的连接。
答案 0 :(得分:1)
再次检查你的故事板,在那里你应该找到名为'name'的孤儿IBOutlet
,删除它并构建。它应该工作正常。祝你好运!