NSInternalInconsistencyException',原因:'试图将0行插入到0,但更新后0节只有0行'

我正在使用UITableViewController并更新tableView时得到此错误。 以下是我的代码:

这发生在我点击事件时:

[timeZoneNames insertObject:@"HELLO" atIndex:0]; [self.tableView beginUpdates]; NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop]; [self.tableView endUpdates]; 

我试图寻找苹果文件,但没有帮助。

谢谢,Aby

我之前遇到过这个问题。 这意味着当-insertRowsAtIndexPaths:withRowAnimation:被调用时-tableView:numberOfRowsInSection:返回0.您需要在调用-insertRowsAtIndexPaths:withRowAnimation:之前插入模型-insertRowsAtIndexPaths:withRowAnimation:请参阅: -beginUpdates


更新

我不知道-tableView:numberOfRowsInSection:的返回值是什么? 另外,如果只有一个更新,则不需要-beginUpdates / -endUpdates

 [timeZoneNames insertObject:@"HELLO" atIndex:0]; // Let's see what the tableView claims is the the number of rows. NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView numberOfRowsInSection:0]); NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop]; 

当我忘记正确设置数据源sockets时,我遇到了这个错误。

如果你看到这个错误,检查你是否明确地设置了你的TableView委托和数据源:

  • 去你的界面生成器,看看与“表视图”

  • cmd +鼠标右键拖拽(你应该看到一条蓝线)从“表格视图”图标到文件的标题图标,在xcode 6中它旁边有一个黄色的图标。

  • 释放你的鼠标,你应该看到委托和数据源选项。

  • select“数据源”

你的桌子现在应该正确连线。

我试图打印出有多less行和部分实际上是在更新的tableView中,让我想,我有多less部分在表视图数据源方法返回…这是罪魁祸首:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 0; } 

确保你正在返回1而不是0。

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } 

至less这个解决了我的问题…

更新:

工作了一段时间之后,我又遇到了同样的问题。 我已经修改了一下应用程序。 我的第一个视图是一个tableView,当你点击右上angular的加号button,你会得到另一个表格,你可以input信息。 当你点击完成后,这个信息被添加到核心数据模型中:

 - (IBAction)doneButtonPressed:(UIBarButtonItem *)sender { MoneyBadgeAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; NSManagedObject *newMoment; newMoment = [NSEntityDescription insertNewObjectForEntityForName:@"SpendingMoment" inManagedObjectContext:context]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyyMMdd"]; NSDate *modifiedDate = [dateFormat dateFromString: self.dateTextField.text]; [newMoment setValue: modifiedDate forKey:@"date"]; [newMoment setValue: descTextField.text forKey:@"desc"]; [appDelegate.eventsArray insertObject:newMoment atIndex:0]; NSError *error; [context save:&error]; [self dismissViewControllerAnimated:YES completion:nil]; } 

我确认它通过在返回到第一个屏幕的viewDidAppear方法中打印到控制台,成功放入Core Data模型。

 -(void)viewDidAppear:(BOOL)animated{ NSEntityDescription *entity = [NSEntityDescription entityForName:@"SpendingMoment" inManagedObjectContext:self.managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:entity]; NSError *error; NSArray *items = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; for (SpendingMoment *moment in items) { NSLog(@"Date: %@, Desc: %@", [moment date], [moment desc]); } [self addEvent]; 

然后我的addEvent方法做到这一点:

 - (void)addEvent { [self.tableScreen beginUpdates]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableScreen insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableScreen reloadData]; [self.tableScreen endUpdates]; @try{ [self.tableScreen scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; } @catch(NSException *exception){ } 

}

任何想法这个问题是什么? 谢谢!

选中“尝试将0行插入到0节,但更新后0节只有0行”。

当您在第0行第0行中插入对象时,它会发出信息,没有任何部分。 尝试在插入行之前插入部分。

  [timeZoneNames insertObject:@"HELLO" atIndex:0]; NSLog(@"sections: %lu ",[self.downlaodTableView numberOfSections]; // insert section [self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationNone]; NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView numberOfRowsInSection:0]); NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]]; [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];