1

嗨,在我的应用程序中,我有一个 TableView,我在其中显示了一些东西,我想在这个 TableView 中搜索我在搜索栏中写的文本,所以我按照本教程进行操作。在我的应用程序中我遇到了问题,当我运行应用程序并尝试搜索它崩溃的字母时,在 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 控制器的连接。

WasteTableViewController 的连接

4

1 回答 1

1

再次检查您的故事板,您应该在那里找到IBOutlet名为“name”的孤儿,将其删除并构建。它应该可以正常工作。祝你好运!

于 2013-09-02T11:02:17.703 回答