Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios TableView或FetchedResultController错误-获取空行_Ios_Uitableview_Nsfetchedresultscontroller - Fatal编程技术网

Ios TableView或FetchedResultController错误-获取空行

Ios TableView或FetchedResultController错误-获取空行,ios,uitableview,nsfetchedresultscontroller,Ios,Uitableview,Nsfetchedresultscontroller,我有一个简单的应用程序,可以创建一些对象。一切正常,但如果我按下打开detailViewController的add按钮,我会按cancel而不是保存它,它会返回到de mainTableView,但它会显示一个空行;如果我打开sql文件,则没有空对象,因此fetchedResultController和tableView加载它之间应该存在问题 我知道这不是添加对象的最佳方式,因为在我的添加操作中,即使用户不保存,我也会创建一个实体。然而,它不应该显示空白行。为什么会这样?如何修复 这是我的代码

我有一个简单的应用程序,可以创建一些对象。一切正常,但如果我按下打开detailViewController的add按钮,我会按cancel而不是保存它,它会返回到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
@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
deataingredientlviewcontroller.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
@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

您的空行对应于您在AddingCredit中创建的NSManagedObject。 即使未保存:NSManagedObjectContext,它仍然存在,并由NSFetchedResultsController拾取

当用户取消添加时,您需要将其删除。也许可以向代理添加控制器:UIViewController*控制器DIDCanceAddingCredit:方法


此外,在controller:UIViewController*controller-didsaveinforment:您不需要在UITableView上重新加载数据,这就是所有NSFetchedResultsControllerDelegate代码为您所做的。

如果我没有添加刚添加的第一个元素的重新加载日期,它将不可见,但如果我单击该行,元素就会出现。为什么?控制器:NSFetchedResultsController*控制器didChangeObject:。。。在某个时候没有使用NSFetchedResultsChangeUpdate类型调用?