ios NSInternalInconsistencyException',原因:'尝试将第 0 行插入第 0 节,但更新后第 0 节中只有 0 行'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11706254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:29:16  来源:igfitidea点击:

NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'

objective-ciosuitableview

提问by insomiac

I am using UITableViewController and getting this error while updating tableView. Below is my code:

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

This occurs when i do a click event:

当我执行点击事件时会发生这种情况:

[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];

I tried looking for apple documentation but that didnt helped.

我尝试寻找苹果文档,但这没有帮助。

Thanks, Aby

谢谢,阿比

采纳答案by Jeffery Thomas

I've ran into this problem before. It means that when -insertRowsAtIndexPaths:withRowAnimation:was called -tableView:numberOfRowsInSection:returned 0. You need to insert into the model before you call -insertRowsAtIndexPaths:withRowAnimation:(see: -beginUpdates)

我以前遇到过这个问题。这意味着,当-insertRowsAtIndexPaths:withRowAnimation:被称为-tableView:numberOfRowsInSection:返回0。你需要插入到模型打电话之前-insertRowsAtIndexPaths:withRowAnimation:(见-beginUpdates



Update

更新

I wonder what the return value of -tableView:numberOfRowsInSection:is? Also, you don't need -beginUpdates/-endUpdatesif you only have one update.

我想知道返回值-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];

回答by NiallJG

I ran into this error when I forgot to set the datasource outlet properly.

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

If you see this error, check that you have explicitly set your TableView delegate and datasource :

如果您看到此错误,请检查您是否已明确设置 TableView 委托和数据源:

  • go to your interface builder and look at the view with the 'Table View'

  • cmd + right click drag ( you should see a blue line ) from the 'Table View' icon to the title icon for the file, this has a yellow icon next to it in xcode 6.

  • release your mouse , you should see delegate and datasource options.

  • select 'datasource'

  • 转到您的界面构建器并查看带有“表格视图”的视图

  • CMD +右键拖动(你应该看到一个蓝线)从“表视图”图标为文件标题图标,这旁边有一个黄色的图标在Xcode 6。

  • 松开鼠标,您应该会看到委托和数据源选项。

  • 选择“数据源”

your table should now be correctly wired.

您的桌子现在应该正确接线。

回答by Angie

I tried printing out how many rows and sections were actually in the tableView during the update which got me thinking, how many sections am I returning in the table view data source method...and this was the culprit:

我试着打印出多行和部分如何在其中得到了我的思维,有多少部分我会在表视图数据源方法返回更新期间的tableView实际上是...这是罪魁祸首:

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

Make sure you are returning 1 and not 0.

确保返回 1 而不是 0。

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

At least this solved the problem for me...

至少这为我解决了问题......

UPDATE:

更新:

I'm having the same problem again after it was working for a while. I had modified the app a bit. My first view is a tableView and when you click the plus button on the upper right, you get another table where you can input information. When you click done, the information is added to the Core Data model with this code:

工作一段时间后,我又遇到了同样的问题。我对应用程序做了一些修改。我的第一个视图是一个 tableView,当您单击右上角的加号按钮时,您会得到另一个表,您可以在其中输入信息。单击完成后,信息将通过以下代码添加到 Core Data 模型中:

- (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];

}

And I confirmed it gets put into the Core Data model successfully by printing to the console the following in my viewDidAppear method upon returning back to the first screen.

我通过在返回到第一个屏幕时在我的 viewDidAppear 方法中将以下内容打印到控制台来确认它已成功放入核心数据模型。

-(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];

And then my addEvent method does this:

然后我的 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){
}

}

}

Any idea what the problem is this time??? Thanks!

知道这次是什么问题吗???谢谢!

回答by ChokWah

Check this "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update".

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

It mesns when you insert object in row 0 , section 0, there are no any sections. Try to insert section before you insert row.

当您插入行0,部分0对象,没有任何部分之mesns。在插入行之前尝试插入节。

 [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];