核心数据示例,无效更新:部分0中的行数无效 [英] Core Data example, Invalid update: invalid number of rows in section 0

查看:179
本文介绍了核心数据示例,无效更新:部分0中的行数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手在iOS和XCode,我正在通过一个Core Data教程,我真的很累。我不断收到以下错误日志中的错误:

I am novice at iOS and XCode, and am working through a Core Data tutorial and am really stumped. I continually get the error in the error logs of the following:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Crashed Thread:  0  Dispatch queue: com.apple.main-thread

Application Specific Information:
iPhone Simulator 235, iPhone OS 4.2 (iPad/8C134)
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '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).'
*** Call stack at first throw:
(
        0   CoreFoundation                      0x010a6be9 __exceptionPreprocess + 185
        1   libobjc.A.dylib                     0x00e9b5c2 objc_exception_throw + 47
        2   CoreFoundation                      0x0105f628 +[NSException raise:format:arguments:] + 136
        3   Foundation                          0x000b447b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
        4   UIKit                               0x00336a0f -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8424
        5   UIKit                               0x00325f81 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 56
        6   SimpleRes                           0x00002496 -[RootViewController addReservation] + 465
        7   UIKit                               0x002b9a6e -[UIApplication sendAction:to:from:forEvent:] + 119
        8   UIKit                               0x004c7167 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
        9   UIKit                               0x002b9a6e -[UIApplication sendAction:to:from:forEvent:] + 119
        10  UIKit                               0x003481b5 -[UIControl sendAction:to:forEvent:] + 67
        11  UIKit                               0x0034a647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
        12  UIKit                               0x003491f4 -[UIControl touchesEnded:withEvent:] + 458
        13  UIKit                               0x002de0d1 -[UIWindow _sendTouchesForEvent:] + 567
        14  UIKit                               0x002bf37a -[UIApplication sendEvent:] + 447
        15  UIKit                               0x002c4732 _UIApplicationHandleEvent + 7576
        16  GraphicsServices                    0x018bda36 PurpleEventCallback + 1550
        17  CoreFoundation                      0x01088064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
        18  CoreFoundation                      0x00fe86f7 __CFRunLoopDoSource1 + 215
        19  CoreFoundation                      0x00fe5983 __CFRunLoopRun + 979
        20  CoreFoundation                      0x00fe5240 CFRunLoopRunSpecific + 208
        21  CoreFoundation                      0x00fe5161 CFRunLoopRunInMode + 97
        22  GraphicsServices                    0x018bc268 GSEventRunModal + 217
        23  GraphicsServices                    0x018bc32d GSEventRun + 115
        24  UIKit                               0x002c842e UIApplicationMain + 1160
        25  SimpleRes                           0x00001ab0 main + 102
        26  SimpleRes                           0x00001a41 start + 53
)

这是我的addReservation代码:

Here is my addReservation code:

-(void)addReservation{

    // Create and configure a new instance of the Event entity.
    Reservations *reservation = (Reservations *)[NSEntityDescription insertNewObjectForEntityForName:@"Reservations" inManagedObjectContext:managedObjectContext];

    [reservation setEnd_Time: [[NSDate alloc]init]];
    [reservation setReservation_Date:[[NSDate alloc]init]];
    [reservation setStart_Time:[NSDate date]];
    [reservation setParty_Size: [NSNumber numberWithInt:4]];
    [reservation setPhone_Number: [NSNumber numberWithInt: 1234567890]];
    [reservation setName: @"Keith"];
    [reservation setNotes: @"He's really hot!"];
    [reservation setPerson_Responsible: @"Lisa"];
    [reservation setTables_Excluded: @"3,5,8"];

    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
        }

    [resArray insertObject:reservation atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                          withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}



我相信我的resArray没有从我输入的方法

I believe that my resArray is not getting the data from the method that I input in programatically, but I am not sure, and I am not sure how to fix it.

任何帮助是值得赞赏的。

Any help is appreciated.

推荐答案

我会看看你的实现

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

从numberOfRowsInSection返回的值不等于上一个值加上使用insertRowsAtIndexPaths添加的行数。如果numberOfRowsInSection的实现使用[myArray count],那么请确保使用的数组实例与要添加预留条目的数组实例相同。如果你没有实现这个方法,那将是你的问题。下面是一个示例:

The error you specified occurs when the value returned from numberOfRowsInSection does not equal the previous value plus the number of rows you added using insertRowsAtIndexPaths. If your implementation of numberOfRowsInSection is using [myArray count], then make sure that the array instance used is the same one into which you are adding the reservation entry. If you have not implemented this method, then that would be your problem. Here is an example of how that method should look:

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    return [resArray count];
}

作为备份,您可以添加列出数组大小的日志语句前后加法,和里面的numberOfRowsInSection函数。

As a backup, you can add log statements that list the size of the array before and after the addition, and inside of the numberOfRowsInSection function.

这篇关于核心数据示例,无效更新:部分0中的行数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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