1

我有一个创建一些对象的简单应用程序。一切正常,但如果我按下打开 detailViewController 的添加按钮,我将按下取消而不是保存它返回到 de mainTableView 但它显示一个空白行,如果我打开 sql 文件没有空对象,所以它应该是fetchedResultController 和加载它的 tableView 之间的问题。

我知道这不是添加对象的最佳方式,因为在我的添加操作中,即使用户不会保存,我也会创建一个实体。然而它不应该显示空白行。为什么会这样?怎么修?

这是我的代码:

IngredietnViewController.m

#import "IngredientsTableViewController.h"
@interface IngredientsTableViewController ()
@end
@implementation IngredientsTableViewController

@synthesize managedObjectContext = _managedObjectContext;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize ingredient = _ingredient;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Ingredients";

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addIngredient)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    self.tableView.tableFooterView = [[UIView alloc] init];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    NSLog(@"%lu", (unsigned long)[sectionInfo numberOfObjects]);
    return [sectionInfo numberOfObjects];
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

#pragma mark - Action

-(void)addIngredient{

    DetailIngredientViewController *detailIngredientVC =[[DetailIngredientViewController alloc]initWithNibName:@"DetailIngredientViewController" bundle:nil];
    detailIngredientVC.delegate = self;

    self.ingredient = [NSEntityDescription insertNewObjectForEntityForName:@"Ingredient" inManagedObjectContext:self.managedObjectContext];
    detailIngredientVC.detailIngredient = self.ingredient;
    [self.navigationController pushViewController:detailIngredientVC animated:YES];


}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{}

#pragma mark - Fetched Result
- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Ingredient" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];

    [fetchRequest setSortDescriptors:sortDescriptors];

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}


#pragma mark - configureCell:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    Ingredient *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = object.name ;
    cell.detailTextLabel.text = object.rappresentation;
}

-(void)controller:(UIViewController *)controller didSaveIngredient:(Ingredient *)DetailIngredient{
    NSLog(@"INGREDIENT SAVED");
    NSError *error = nil;
    if (![self.managedObjectContext save:&error]){
        NSLog(@"Error %@ %@", error, [error userInfo]);
    }
    [self.tableView reloadData];
}

@end

DeataiIngredientlViewController.m

@implementation DetailIngredientViewController

@synthesize tfNameIngredient, detailIngredient, tvDescription;

- (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 from its nib.
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveIngredient)];
    self.navigationItem.rightBarButtonItem = saveButton;
}

-(void)saveIngredient{
    if ([self verifyIngredient] == YES){
        detailIngredient.name = tfNameIngredient.text;
        detailIngredient.rappresentation = tvDescription.text;
        [self.delegate controller:self didSaveIngredient:detailIngredient];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else{
       NSLog(@"SBALLATA");
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The Name and description are empty" delegate:self cancelButtonTitle:@"Back" otherButtonTitles:nil, nil];
        [alert show];
    }
}


-(BOOL)verifyIngredient{
       if ([tfNameIngredient.text length] < 4 || [tvDescription.text length] < 4 ){
        return NO;
    }
    else{
        return YES;
    }
}

@end
4

1 回答 1

0

您的空白行对应于Ingredient NSManagedObject您在 中创建的addIngredient。即使您没有save:NSManagedObjectContext它仍然存在并被您的NSFetchedResultsController.

当用户cancels添加时,您需要将其删除。也许controller:(UIViewController *)controller didCancelAddIngredient:为您的委托添加一个方法?

此外,controller:(UIViewController *)controller didSaveIngredient:您不需要reloadData在您UITableViewNSFetchedResultsControllerDelegate.

于 2013-09-06T09:45:25.890 回答