在cell.textLabel.text上显示NSManagerObject的正确值



我是Xcode 5的新手,开始用appcode.com的教程做一些编码。我不能得到正确的搜索结果打印回tableview(core data),任何建议将非常感激,提前感谢!欢呼。

#import "RecipeStoreTableViewController.h"
#import "AddRecipeViewController.h"
@interface RecipeStoreTableViewController () {
    NSMutableArray *recipes;
    NSArray *searchResults;
}
@end
@implementation RecipeStoreTableViewController

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // Fetch the recipes from persistent data store
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Recipe"];
    recipes = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    //  NSLog(@"test %@", [recipes valueForKey:@"name"]);

    // Reload table data
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];
    }else{
        return [recipes count];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...
    NSManagedObject *recipe = [recipes objectAtIndex:indexPath.row];
    if (tableView == self.searchDisplayController.searchResultsTableView) {
      cell.textLabel.text = [searchResults valueForKey:@"name"];
       NSString *description = [[NSString alloc] initWithFormat:@"%@  - %@", [searchResults valueForKey:@"image"], [searchResults valueForKey:@"prepTime"]];
      cell.detailTextLabel.text = description;
    }else{
        cell.textLabel.text = [recipe valueForKey:@"name"];
        NSString *description = [[NSString alloc] initWithFormat:@"%@  - %@", [recipe valueForKey:@"image"], [recipe valueForKey:@"prepTime"]];
        cell.detailTextLabel.text = description;
    }  
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context = [self managedObjectContext];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete object from database
        [context deleteObject:[recipes objectAtIndex:indexPath.row]];
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }
        // Remove recipe from table view
        [recipes removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"UpdateRecipe"]) {
        NSManagedObject *selectedRecipe = [recipes objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
        UINavigationController *destViewController = segue.destinationViewController;
        AddRecipeViewController *recipeViewController = (AddRecipeViewController*)destViewController.topViewController;
        recipeViewController.recipe = selectedRecipe;
    }
}
-(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;
}
@end

你在NSMutableArray对象上调用valueForKey,它应该在NSManagedObject上调用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...
    NSManagedObject *recipe;
    if (tableView == self.searchDisplayController.searchResultsTableView)
        recipe = searchResults[indexPath.row];
    else
        recipe = recipes[indexPath.row];
    cell.textLabel.text = [recipe valueForKey:@"name"];
    NSString *description = [[NSString alloc] initWithFormat:@"%@  - %@", [recipe valueForKey:@"image"], [recipe valueForKey:@"prepTime"]];
    cell.detailTextLabel.text = description;
    return cell;
}

最新更新