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

ありがとうございました!

更新 ここに、テーブルビューコントローラーの接続を確認できるストーリーボードの画面を投稿します。

WasteTableViewController の接続

4

1 に答える 1

1

IBOutletストーリーボードをもう一度確認すると、'name' という名前の孤児が見つかるはずです。それを削除してビルドします。それはうまくいくはずです。幸運を!

于 2013-09-02T11:02:17.703 に答える