2011-02-18 62 views
4

我是iOS和XCode的新手,並且正在通過核心數據教程工作,我真的難住了。我不斷得到下面的錯誤日誌中的錯誤:核心數據示例,無效更新:第0節中的行數無效

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代碼:

-(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不從的方法,我輸入在獲取數據以編程方式,但我不確定,我不知道如何解決它。

任何幫助表示讚賞。

+0

如果您遵循Apple iOS Core Data教程,則模板生成的文件將返回0;爲numberOfSectionsInTable委託方法。只要改變這條線返回1,它應該是好的。 – Zhang

回答

30

我會在你執行

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

您指定的錯誤發生時看看當從numberOfRowsInSection返回的值不等於以前的值加上您添加使用insertRowsAtIndexPaths的行數。如果您的numberOfRowsInSection實現正在使用[myArray count],那麼請確保使用的數組實例與添加預留條目的實例相同。如果你還沒有實現這個方法,那就是你的問題。這裏是一個方法應該如何看一個例子:

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

作爲備份,你可以添加前和添加後列出數組的大小日誌報表,以及numberOfRowsInSection函數內。

+0

如果你像我一樣愚蠢,數組可能爲空。 :) –

11

您從tableView中刪除/插入並從基礎數據源刪除/插入的順序可能很重要。 Apple文檔首先更新視圖,然後更新數據源。但是,從視圖中刪除/插入內部調用- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section,如果您像我一樣返回底層數據源的count。因爲這沒有改變,所以運行時會抱怨並崩潰。所以先從數據源中刪除,然後再刪除視圖。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [recentImages count]; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Delete from underlying data source first! 
    recentImages = [recentImages removeObjectAtIndex:indexPath.row]; 

    // Then perform the action on the tableView 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
          withRowAnimation:UITableViewRowAnimationFade];   
     [tableView endUpdates]; 
    } 

    // Finally, reload data in view 
    [self.tableView reloadData]; 
} 
+0

最後對'reloadData'的調用是多餘的。您應該使用'begin' /'endUpdates'來將表同步到您的基礎數據*或*'reloadData',以重新加載所有內容(這對CPU來說代價更高,可能對用戶來說看起來不太好)。但是沒有必要這樣做。我提交了一個修改。 –

0

對我來說,看起來好像在插入對象之前,您應該對其進行相應的更改。我可能是錯的,因爲我也是新手!

0
  • (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分{

    return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; 
    

    }

  • (無效)的tableView:(UITableView的*)的tableView commitEditingStyle :(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    如果(editingStyle == UITableViewCellEditingStyleDelete){

    [self.tableView beginUpdates]; 
    
    // Delete the object that was swiped 
    YourTableName *objectToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    NSLog(@"Deleting (%@)", objectToDelete.fieldname); 
    [self.managedObjectContext deleteObject: objectToDelete]; 
    [self.managedObjectContext save:nil]; 
    
    // Delete the (now empty) row on the table 
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    
    
    
    [self.tableView reloadData]; 
    
    [self.tableView endUpdates]; 
    
    
        } 
    

    }

刪除和添加記錄與fetchedResultsController核心數據表時,這工作。

相關問題