当我使用添加函数时,声明失败 [英] Assertion failure when i use the Add Function

查看:113
本文介绍了当我使用添加函数时,声明失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表中,我有3个索引,每个函数调用。在每个函数中,有一个添加按钮,将数据添加到CoreData中。前2个工作,但是当我按第三个索引并按添加,这个错误出现。

  2011-07-19 16: 57:11.079 CoreDataMelaka [2704:207] * **  -   -  [UITableView _endCellAnimationsWithContext:]中的声明失败,/SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976** 
2011-07-19 16 :57:11.080 CoreDataMelaka [2704:207] **严重应用程序错误。在核心数据更改处理期间捕获异常。这通常是NSManagedObjectContextObjectsDidChangeNotification的观察器中的错误。无效更新:部分0中的行数无效。更新(0)后现有段中包含的行数必须等于更新前该部分中包含的行数(0),加或减数从该部分插入或删除的行(插入1个,删除0个)。 with userInfo(null)****

这里是第三类的代码,我调用

  @implementation MainFoodsInfoController 

@synthesize mainfoods;
@synthesize mainfoodsarray;
@synthesize fetchedResultsController;
@synthesize tableView;
@synthesize foodsbutton;
@synthesize editfoodsbutton;
@synthesize addfoodsbutton;


- (IBAction)foodslist:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}


- (IBAction)addmainfoods:(id)sender
{
NSManagedObjectContext * context = [self.fetchedResultsController managedObjectContext];
AddMainFoodsController * addmainfoodsController = [[AddMainFoodsController alloc] initWithNibName:@AddMainFoodsControllerbundle:nil];
addmainfoodsController.delegate = self;
addmainfoodsController.mainfoods = [NSEntityDescription insertNewObjectForEntityForName:@MainFoodsinManagedObjectContext:context];
UINavigationController * navigatController = [[UINavigationController alloc] initWithRootViewController:addmainfoodsController];
[self presentModalViewController:navigatController animated:YES];
[addmainfoodsController release];
[navigatController release];
}


- (IBAction)editmainfoods:(id)sender
{
if([editfoodsbutton.title isEqualToString:@Edit])
{
editfoodsbutton.title = @完成
[self.tableView setEditing:YES animated:YES];
}
else
{
editfoodsbutton.title = @编辑;
[self.tableView setEditing:NO animated:YES];
}
}

- (void)addmainfoodsController:(AddMainFoodsController *)controller selectedsave:(BOOL)save
{
NSManagedObjectContext * context = .fetchedResultsController managedObjectContext];
if(!save){
[context deleteObject:controller.mainfoods];
}
NSError * error; if(![context save:& error])
{
NSLog(@未解析的错误%@%@,错误,[错误userInfo]); exit(-1);
}
[self dismissModalViewControllerAnimated:YES];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSInteger numofRows = 0;
if([[self.fetchedResultsController sections] count]> 0)
{
id< NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
numofRows = [sectionInfo numberOfObjects];
}
return numofRows;
}

//自定义表视图单元格的外观。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @Cell;
UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

//配置单元格。
NSManagedObject * managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[managedObject valueForKey:@foodsname] description];
UIImage * pimage = [managedObject valueForKey:@foodstableimage];
CGSize size = pimage.size;
CGFloat ratio = 0; if(size.width> size.height)
{
ratio = 44.0 / size.width;
}
else
{
ratio = 44.0 / size.height;
}
CGRect rect = CGRectMake(0.0,0.0,ratio * size.width,ratio * size。height);
UIGraphicsBeginImageContext(rect.size);
[pimage drawInRect:rect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
EditMainFoodsController * editmainfoodsController = [[EditMainFoodsController alloc] initWithNibName :@EditMainFoodsControllerbundle:nil];
MainFoods * selectedmainfoods =(MainFoods *)[fetchedResultsController objectAtIndexPath:indexPath];
editmainfoodsController.mainfoods = selectedmainfoods;
UINavigationController * naviggController = [[UINavigationController alloc] initWithRootViewController:editmainfoodsController];
[self presentModalViewController:naviggController animated:YES];
}

//覆盖以支持编辑表视图。
- (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; if(![context save:& error])
{
//处理错误...
}
}
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSManagedObjectContext * context = [self.fetchedResultsController managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@MainFoodsinManagedObjectContext:context]];
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@foodsnameascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
self.tableView.rowHeight = 44.0;
NSError * error = nil;
mainfoodsarray =(NSMutableArray *)[context executeFetchRequest:fetchRequest error:& error];
[sortDescriptor release];
[sortDescriptors release];
[fetchRequest release];
[self.tableView reloadData];
}

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @Foods Info;
NSError * error;
if(![[self fetchedResultsController] performFetch:& error]){
//处理错误...
}


}

- (NSFetchedResultsController *)fetchedResultsController {
if(fetchedResultsController!= nil){
return fetchedResultsController;
}

CoreDataMelakaAppDelegate * appDelegate =(CoreDataMelakaAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [appDelegate managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@MainFoodsinManagedObjectContext:context]];
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@foodsnameascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@Root];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
return fetchedResultsController; (NSFetchedResultsChangeType)类型newIndexPath:(NSIndexPath *)newIndexPath(NSIndexPath *)newIndexPath(NSIndexPath *)newIndexPath(NSIndexPath *)newIndexPath(NSIndexPath *)newIndexPath(NSFetchedResultsController *)控制器didChangeObject: {
switch(type){
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}

- (void)didReceiveMemoryWarning {
//如果没有超级视图,则释放视图。
[super didReceiveMemoryWarning];

//释放所有未使用的缓存数据,图像等。
}

- (void)viewDidUnload {
[super viewDidUnload];
//释放主视图的任何保留的子视图。
//例如self.myOutlet = nil;
}


- (void)dealloc {
[foodsbutton release];
[editfoodsbutton release];
[addfoodsbutton release];
[tableView release];
[super dealloc];
}

@end

导致崩溃。当我按添加,我想要这个类被调用,但这导致崩溃,其他2类没有问题只有这有问题

  @implementation AddMainFoodsController 

@synthesize savebutton;
@synthesize cancelbutton;
@synthesize delegate;
@synthesize foodsnewname;
@synthesize foodsnewinfo;
@synthesize foodsnewemailid;
@synthesize foodsnewcontactno;
@synthesize foodsimage1;
@synthesize foodsimage2;
@synthesize foodsimage3;
@synthesize foodstableimage;
@synthesize mainfoods;
@synthesize tempImage;

NSInteger countaddfoods = 0;

- (IBAction)cancel:(id)sender
{
[delegate addmainfoodsController:self selectedsave:NO];
}

- (IBAction)save:(id)sender
{
mainfoods.foodsname = foodsnewname.text;
mainfoods.foodsemailid = foodsnewemailid.text;
mainfoods.foodscontactno = foodsnewcontactno.text;
mainfoods.foodsinfo = foodsnewinfo.text;
[delegate addmainfoodsController:self selectedsave:YES];
}

- (IBAction)selectimagebutton1:(id)sender
{
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}

- (IBAction)selectimagebutton2:(id)sender
{
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}

- (IBAction)selectimagebutton3:(id)sender
{
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}

- (IBAction)selecttableimagebutton:(id)sender
{
UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
{
tempImage = selectedImage;
countaddfoods = countaddfoods + 1;

[self setImage];
[self dismissModalViewControllerAnimated:YES];
}

- (void)setImage
{
if(countaddfoods == 1)
{
foodsimage1.image = tempImage;
mainfoods.foodsimage1 = tempImage;
}
else if(countaddfoods == 2)
{
foodsimage2.image = tempImage;
mainfoods.foodsimage2 = tempImage;
}
else if(countaddfoods == 3)
{
foodsimage3.image = tempImage
mainfoods.foodsimage3 = tempImage;
}
else
{
foodstableimage.image = tempImage;
mainfoods.foodstableimage = tempImage;
}
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
[super viewDidLoad]; self.title = @新食品;
self.navigationItem.rightBarButtonItem = savebutton;
self.navigationItem.leftBarButtonItem = cancelbutton;
}

- (void)dealloc
{
[savebutton release];
[cancelbutton release];
[foodsnewname release];
[foodsnewemailid release];
[foodsnewcontactno release];
[foodsnewinfo release];
[foodsimage1 release];
[foodsimage2 release];
[foodsimage3 release];
[foodstableimage release];
[mainfoods release];
[tempImage release];
[super dealloc];
}

@end


解决方案>

错误讯息告诉您问题:


无效的更新:区段0中行数无效。$ b更新(0)之后的现有段中包含的$ b行必须为
等于
更新(0)之前该段中包含的行数,加或减插入的行数,从
中删除该节(插入1,删除0)。在您的表部分(0)中,您开始使用零行数(0)。在表部分(0)中,使用userInfo(null) * *




<即空段。然后你添加了1行,所以section(0)row count应该是1,但 numberOfRowsInSection:返回零。



这个问题有三个可能的原因:


  1. 使用 controllerWillChangeContent:中的 beginUpdate 冻结表视图,以便表格视图在更新完成之前尝试重新绘制。

  2. 您的 numberOfRowsInSection 会返回该部分的错误行数。



  3. 无法告诉您没有代码的实际问题。


    In a table, I have 3 indexes which calls each a function. In each function, there is an add button that adds data into CoreData. First 2 works, but when i press the 3rd index and press Add , this error appears.

    2011-07-19 16:57:11.079 CoreDataMelaka[2704:207] * **Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976**
    2011-07-19 16:57:11.080 CoreDataMelaka[2704:207] **Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)****
    

    here's the code for 3rd class that i call

    @implementation MainFoodsInfoController
    
    @synthesize mainfoods;
    @synthesize mainfoodsarray;
    @synthesize fetchedResultsController;
    @synthesize tableView;
    @synthesize foodsbutton;
    @synthesize editfoodsbutton;
    @synthesize addfoodsbutton;
    
    
    -(IBAction) foodslist:(id) sender 
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    
    
    -(IBAction) addmainfoods:(id)sender 
    {
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        AddMainFoodsController *addmainfoodsController=[[AddMainFoodsController alloc] initWithNibName:@"AddMainFoodsController" bundle:nil];
        addmainfoodsController.delegate=self;   
        addmainfoodsController.mainfoods =  [NSEntityDescription insertNewObjectForEntityForName:@"MainFoods" inManagedObjectContext:context];
        UINavigationController *navigatController=[[UINavigationController alloc] initWithRootViewController:addmainfoodsController];
        [self   presentModalViewController:navigatController animated:YES]; 
        [addmainfoodsController release];
        [navigatController release];
    }
    
    
    -(IBAction) editmainfoods:(id)sender 
    {
        if([editfoodsbutton.title isEqualToString:@"Edit"]) 
        {
            editfoodsbutton.title=@"Done"; 
            [self.tableView setEditing:YES animated:YES];
        } 
        else 
        {
            editfoodsbutton.title=@"Edit"; 
            [self.tableView setEditing:NO animated:YES];
        }
    }
    
    -(void) addmainfoodsController:(AddMainFoodsController *)controller selectedsave:(BOOL) save 
    {
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        if(!save) {
            [context deleteObject:controller.mainfoods];
        } 
        NSError *error; if(![context save:&error])
        {
            NSLog(@"Unresolved error %@ %@", error, [error userInfo]); exit(-1);
        } 
        [self dismissModalViewControllerAnimated:YES];
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    {
        NSInteger count = [[self.fetchedResultsController sections] count];
        if (count == 0) 
        {
            count = 1;
        }
        return count;   
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
        NSInteger numofRows = 0;
        if ([[self.fetchedResultsController sections] count] > 0) 
        {
            id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
            numofRows = [sectionInfo numberOfObjects];
        }
        return numofRows;
    }
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
        { 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        }    
    
        // Configure the cell.
        NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
        cell.textLabel.text = [[managedObject valueForKey:@"foodsname"] description];
        UIImage *pimage=[managedObject valueForKey:@"foodstableimage"]; 
        CGSize size=pimage.size; 
        CGFloat ratio = 0; if (size.width > size.height) 
        {
            ratio = 44.0 / size.width;
        } 
        else 
        {
            ratio = 44.0 / size.height;
        }
        CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size. height);
        UIGraphicsBeginImageContext(rect.size); 
        [pimage drawInRect:rect]; 
        cell.imageView.image=UIGraphicsGetImageFromCurrentImageContext(); 
        UIGraphicsEndImageContext(); 
        return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        EditMainFoodsController *editmainfoodsController = [[EditMainFoodsController alloc] initWithNibName:@"EditMainFoodsController" bundle:nil];
        MainFoods *selectedmainfoods = (MainFoods *) [fetchedResultsController objectAtIndexPath:indexPath];
        editmainfoodsController.mainfoods= selectedmainfoods;
        UINavigationController *naviggController=[[UINavigationController alloc] initWithRootViewController:editmainfoodsController];
        [self presentModalViewController:naviggController animated:YES];
    }
    
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *) indexPath 
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) 
        { // Delete the managed object for the given index path
            NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
            [context deleteObject:[[self fetchedResultsController] objectAtIndexPath:indexPath]];
            NSError *error; if (![context save:&error]) 
            {
                // Handle the error...
            }
        }   
    }
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"MainFoods" inManagedObjectContext:context]];
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"foodsname" ascending:YES];
        NSArray *sortDescriptors =[[NSArray alloc] initWithObjects:sortDescriptor,nil]; 
        [fetchRequest setSortDescriptors:sortDescriptors];
        self.tableView.rowHeight=44.0;
        NSError *error=nil;
        mainfoodsarray=(NSMutableArray *)[context executeFetchRequest:fetchRequest error:&error];   
        [sortDescriptor release];
        [sortDescriptors release];
        [fetchRequest release];
        [self.tableView reloadData];     
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title=@"Foods Info";
        NSError *error;
        if (![[self fetchedResultsController] performFetch:&error]) {
            // Handle the error...
        }   
    
    
    }
    
    - (NSFetchedResultsController *)fetchedResultsController {
        if (fetchedResultsController != nil) {
            return fetchedResultsController;
        }
    
        CoreDataMelakaAppDelegate *appDelegate = (CoreDataMelakaAppDelegate *)[[UIApplication sharedApplication] delegate];
        NSManagedObjectContext *context = [appDelegate managedObjectContext];
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"MainFoods" inManagedObjectContext:context]];
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"foodsname" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        [fetchRequest setSortDescriptors:sortDescriptors];
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Root"];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;
        [aFetchedResultsController release];
        return fetchedResultsController;
    }    
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
        switch(type) {
            case NSFetchedResultsChangeInsert:
                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;
            case NSFetchedResultsChangeDelete:
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                break;
        }
    }
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    
        // Release any cached data, images, etc. that aren't in use.
    }
    
    - (void)viewDidUnload {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [foodsbutton release];
        [editfoodsbutton release];
        [addfoodsbutton release];
        [tableView release];
        [super dealloc];
    }
    
    @end
    

    and here's the add class that causes the crash. When i press add, i want this class to be called but this causes a crash, the other 2 classes had no issue only this has the issue

    @implementation AddMainFoodsController
    
    @synthesize savebutton; 
    @synthesize cancelbutton; 
    @synthesize delegate; 
    @synthesize foodsnewname; 
    @synthesize foodsnewinfo; 
    @synthesize foodsnewemailid;
    @synthesize foodsnewcontactno;
    @synthesize foodsimage1;
    @synthesize foodsimage2;
    @synthesize foodsimage3;
    @synthesize foodstableimage;
    @synthesize mainfoods;
    @synthesize tempImage;
    
    NSInteger countaddfoods = 0;
    
    -(IBAction) cancel:(id) sender 
    {
        [delegate addmainfoodsController: self selectedsave:NO];
    }
    
    -(IBAction) save:(id)sender
    { 
        mainfoods.foodsname = foodsnewname.text; 
        mainfoods.foodsemailid = foodsnewemailid.text; 
        mainfoods.foodscontactno = foodsnewcontactno.text;
        mainfoods.foodsinfo = foodsnewinfo.text;
        [delegate addmainfoodsController: self selectedsave:YES ];
    }
    
    -(IBAction) selectimagebutton1:(id)sender
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
        imagePicker.delegate = self;
        [self presentModalViewController:imagePicker animated:YES];
        [imagePicker release];
    }
    
    -(IBAction) selectimagebutton2:(id)sender
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
        imagePicker.delegate = self;
        [self presentModalViewController:imagePicker animated:YES];
        [imagePicker release];
    }
    
    -(IBAction) selectimagebutton3:(id)sender
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
        imagePicker.delegate = self;
        [self presentModalViewController:imagePicker animated:YES];
        [imagePicker release];
    }
    
    -(IBAction) selecttableimagebutton:(id)sender
    {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
        imagePicker.delegate = self;
        [self presentModalViewController:imagePicker animated:YES];
        [imagePicker release];
    }
    
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo
    {
        tempImage = selectedImage;
        countaddfoods = countaddfoods + 1;
    
        [self setImage];
        [self dismissModalViewControllerAnimated:YES]; 
    }
    
    -(void) setImage
    {
        if (countaddfoods == 1)
        {
            foodsimage1.image = tempImage;
            mainfoods.foodsimage1 = tempImage;
        }
        else if (countaddfoods == 2)
        {
            foodsimage2.image = tempImage;
            mainfoods.foodsimage2 = tempImage;
        }
        else if (countaddfoods == 3)
        {
            foodsimage3.image = tempImage;
            mainfoods.foodsimage3 = tempImage;
        }
        else 
        {
            foodstableimage.image = tempImage;
            mainfoods.foodstableimage = tempImage;
        }
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    
    - (void)viewDidLoad 
    {
        [super viewDidLoad]; self.title=@"New Foods"; 
        self.navigationItem.rightBarButtonItem = savebutton; 
        self.navigationItem.leftBarButtonItem = cancelbutton;
    }
    
    - (void)dealloc 
    { 
        [savebutton release]; 
        [cancelbutton release]; 
        [foodsnewname release]; 
        [foodsnewemailid release]; 
        [foodsnewcontactno release]; 
        [foodsnewinfo release];
        [foodsimage1 release];
        [foodsimage2 release];
        [foodsimage3 release];
        [foodstableimage release];
        [mainfoods release]; 
        [tempImage release];
        [super dealloc];
    } 
    
    @end
    

    解决方案

    The error message tells you the problem:

    Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted). with userInfo (null)**

    In your table section (0), you started with zero rows i.e. an empty section. Then you added 1 row so the section(0) row count should be 1 but numberOfRowsInSection: is returning zero.

    There are three probable causes for this problem:

    1. You didn't freeze the tableview with beginUpdate in controllerWillChangeContent: such that the tableview tries to redraw itself before the update completes.
    2. Your numberOfRowsInSection returns the wrong row count for the section.
    3. You are inserting one managed object multiple times or you have a validate on insert/update that fails.

    Can't tell you the actual problem without the code.

    这篇关于当我使用添加函数时,声明失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆