代码之家  ›  专栏  ›  技术社区  ›  Jackson

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

  •  0
  • Jackson  · 技术社区  · 10 年前

    我是Xcode 5的新手,并开始使用appcode.com的教程进行一些编码。 我无法将正确的搜索结果打印回表视图(核心数据),任何建议都将非常感谢,提前感谢!干杯

    #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
    
    1 回复  |  直到 10 年前
        1
  •  0
  •   Jun    10 年前

    您正在调用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;
    }