简体   繁体   English

NSInternalInconsistencyException(无效更新:第0部分无效的行数)

[英]NSInternalInconsistencyException(Invalid update: invalid number of rows in section 0)

I design the process is that: 我设计的过程是:

  1. Enter into a CourseAddViewController(UITableViewController) via a Model Segue; 通过Model Segue进入CourseAddViewController(UITableViewController);
  2. The right bar button display "Done"(At this status, the tableview display all data in eachrow with "-", but the additional row at the bottom with "+"); 右栏按钮显示“完成”(在此状态下,表格视图的每个行中的所有数据均以“-”显示,而底部的其他行则以“ +”显示);
  3. Press "Done" button, the save the data. 按下“完成”按钮,保存数据。

When I add the statement "self.editing = YES;" 当我添加语句“ self.editing = YES;”时 in - (void)viewDidLoad method, it will crash. 在-(void)viewDidLoad方法中,它将崩溃。

I found that it didn't perform "tableView:cellForRowAtIndexPath:" method.But, actually I trust that there is 1 row in section 0 via the result of NSLog(). 我发现它没有执行“ tableView:cellForRowAtIndexPath:”方法。但是,实际上,我相信通过NSLog()的结果,第0节中有1行。

If I don't add "self.editing = YES;", it run correctly. 如果我不添加“ self.editing = YES;”,则它将正确运行。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.editing = YES;     // here: adding it cause crash
}

After performing "insertRowsAtIndexPaths:withRowAnimation:", it crashed. 执行“ insertRowsAtIndexPaths:withRowAnimation:”后,它崩溃了。 Two statement: 两条声明:

[self.tableView beginUpdates];
[self.tableView endUpdates];

add them or not, all cuase crash. 是否添加它们,都导致崩溃。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    [self.tableView beginUpdates];

    NSInteger booksCount = self.course.books.count;

    NSArray *booksInsertIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:booksCount inSection:0]];

    if (editing) {
        [self.tableView insertRowsAtIndexPaths:booksInsertIndexPath 
                              withRowAnimation:UITableViewRowAnimationBottom];  // then, it crashes
    } else {
        [self.tableView deleteRowsAtIndexPaths:booksInsertIndexPath 
                              withRowAnimation:UITableViewRowAnimationBottom];
    }

    [self.tableView endUpdates];

    // ...

}

Other method, 其他方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%d", self.fetchedResultsController.sections.count);     // the result is always 1;

    return self.fetchedResultsController.sections.count;
}

Other method, 其他方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
    NSInteger rows = [sectionInfo numberOfObjects];
    if (self.editing)
        rows++; 

    NSLog(@"%d", rows);     // the result is always 1;

    return rows;
}

Other method, 其他方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController.sections objectAtIndex:indexPath.section];
    NSInteger rows = [sectionInfo numberOfObjects];
    if (self.editing && rows == indexPath.row) {
        return  cell;
    }
    [self configureCell:cell atIndexPath:indexPath];

//    NSLog(@"%d-%d-%d-%d", self.editing, rows, indexPath.section, indexPath.row);

    return cell;
}

New Edit: 新编辑:

2012-10-27 08:44:29.934 Course Table[306:fb03] *** Assertion failure in
 -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/
UIKit-914.84/UITableView.m:1037
2012-10-27 08:44:29.935 Course Table[306:fb03] CRASH: Invalid update: 
invalid number of rows in section 0.  The number of rows contained in 
an existing section after the update (1) must be equal to the number 
of rows contained in that section before the update (1), plus or 
minus the number of rows inserted or deleted from that section 
(1 inserted, 0 deleted) and plus or minus the number of rows moved
into or out of that section (0 moved in, 0 moved out).
2012-10-27 08:44:29.942 Course Table[306:fb03] Stack Trace: (
    0   CoreFoundation                      0x016cf03e __exceptionPreprocess + 206
    1   libobjc.A.dylib                     0x01860cd6 objc_exception_throw + 44
    2   CoreFoundation                      0x01677a48 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x009d12cb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x000bf3d7 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 12439
    5   UIKit                               0x000ca6d2 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 295
    6   UIKit                               0x000ca711 -[UITableView insertRowsAtIndexPaths:withRowAnimation:] + 55
    7   Course Table                        0x0000ef42 -[CTXCourseAddViewController setEditing:animated:] + 434
    8   UIKit                               0x0010de4c -[UIViewController(UINavigationControllerItem) setEditing:] + 49
    9   Course Table                        0x0000ec1f -[CTXCourseAddViewController viewDidLoad] + 655
    10  UIKit                               0x00101a1e -[UIViewController view] + 184
    11  UIKit                               0x00100fec -[UIViewController nextResponder] + 34
    12  UIKit                               0x00127f1d -[UIResponder _containsResponder:] + 40
    13  UIKit                               0x001121cb -[UINavigationController defaultFirstResponder] + 83
    14  UIKit                               0x00128df1 -[UIResponder(Internal) _deepestDefaultFirstResponder] + 36
    15  UIKit                               0x00128ea9 -[UIResponder(Internal) _promoteDeepestDefaultFirstResponder] + 36
    16  UIKit                               0x00322508 -[UIWindowController transitionViewDidStart:] + 89
    17  UIKit                               0x000df401 -[UITransitionView _didStartTransition] + 93
    18  UIKit                               0x000e008b -[UITransitionView transition:fromView:toView:] + 1169
    19  UIKit                               0x00321d6c -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 6114
    20  UIKit                               0x00108857 -[UIViewController presentViewController:withTransition:completion:] + 3579
    21  UIKit                               0x001089bc -[UIViewController presentViewController:animated:completion:] + 112
    22  UIKit                               0x001089fc -[UIViewController presentModalViewController:animated:] + 56
    23  UIKit                               0x00470f4a -[UIStoryboardModalSegue perform] + 250
    24  UIKit                               0x004654d0 -[UIStoryboardSegueTemplate perform:] + 174
    25  CoreFoundation                      0x016d0e99 -[NSObject performSelector:withObject:withObject:] + 73
    26  UIKit                               0x0003d14e -[UIApplication sendAction:to:from:forEvent:] + 96
    27  UIKit                               0x0027ba0e -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 145
    28  CoreFoundation                      0x016d0e99 -[NSObject performSelector:withObject:withObject:] + 73
    29  UIKit                               0x0003d14e -[UIApplication sendAction:to:from:forEvent:] + 96
    30  UIKit                               0x0003d0e6 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    31  UIKit                               0x000e3ade -[UIControl sendAction:to:forEvent:] + 66
    32  UIKit                               0x000e3fa7 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503
    33  UIKit                               0x000e3266 -[UIControl touchesEnded:withEvent:] + 549
    34  UIKit                               0x000623c0 -[UIWindow _sendTouchesForEvent:] + 513
    35  UIKit                               0x000625e6 -[UIWindow sendEvent:] + 273
    36  UIKit                               0x00048dc4 -[UIApplication sendEvent:] + 464
    37  UIKit                               0x0003c634 _UIApplicationHandleEvent + 8196
    38  GraphicsServices                    0x015b9ef5 PurpleEventCallback + 1274
    39  CoreFoundation                      0x016a3195 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    40  CoreFoundation                      0x01607ff2 __CFRunLoopDoSource1 + 146
    41  CoreFoundation                      0x016068da __CFRunLoopRun + 2218
    42  CoreFoundation                      0x01605d84 CFRunLoopRunSpecific + 212
    43  CoreFoundation                      0x01605c9b CFRunLoopRunInMode + 123
    44  GraphicsServices                    0x015b87d8 GSEventRunModal + 190
    45  GraphicsServices                    0x015b888a GSEventRun + 103
    46  UIKit                               0x0003a626 UIApplicationMain + 1163
    47  Course Table                        0x000020dd main + 141
    48  Course Table                        0x00002045 start + 53
)
2012-10-27 08:44:29.945 Course Table[306:fb03] *** 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 (1) must be equal to the number 
of rows contained in that section before the update (1), plus or minus
the number of rows inserted or deleted from that section (1 inserted,
0 deleted) and plus or minus the number of rows moved into or out of that 
section (0 moved in, 0 moved out).
terminate called throwing an exception(lldb) 

Just add 只需添加

[self.tableView reloadData];

before 之前

self.editing = YES;

This is needed because the table view doesn't initially have information about its data source and delegate; 这是必需的,因为表视图最初不具有有关其数据源和委托的信息。 if you create a table view, it always needs to be sent a reloadData message as part of its initialization. 如果创建表视图,则始终需要向其发送reloadData消息作为其初始化的一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSInternalInconsistencyExceptionException原因:&#39;无效的更新:部分0中的行数无效&#39; - NSInternalInconsistencyException reason: 'Invalid update: invalid number of rows in section 0' 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效” - Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0 如何修复“NSInternalInconsistencyException”,原因:&#39;无效更新:第0节中的行数无效。“ - How to fix “NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.” Tableview更新“ NSInternalInconsistencyException”,原因:“无效的更新:无效的行数? - Tableview update 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows? 无效更新:第0部分UITableView中的行数无效 - Invalid update: invalid number of rows in section 0 UITableView 无效的更新:iOS中第0部分的无效行数 - Invalid update: invalid number of rows in section 0 in iOS iOS 5:更新无效:部分中的行数无效 - ios 5: Invalid update: invalid number of rows in section 无效的更新:部分中的行数无效 - Invalid update: invalid number of rows in section &#39;无效更新:第 0 节中的行数无效 - 'Invalid update: invalid number of rows in section 0 无效的更新:部分中的行数无效 - Invalid update: invalid number of rows in section
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM