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

标签 ios tableview

我是 iOS 编程的新手,我正在尝试制作这款经济型应用。我想将我的预算类的实例类添加到我的 TableView 中。但是当我点击完成按钮时,这个错误出现了。我一直在论坛中搜索我的问题的答案。

到目前为止,我发现 Aby 和我有同样的问题 (http://stackoverflow.com/questions/11706254/nsinternalinconsistencyexception-reason-attempt-to-insert-row-0-into-section?answertab= active#tab-top),但我无法在那篇文章中真正得到我的问题的明确答案/解决方案。

我确定我知道为什么会发生此错误,但我不知道如何解决它:-( 谁能帮我解决这个问题?

我想发布我的代码的一些图片,但由于垃圾邮件安全问题,stackoverflow 不允许我发布。

AddBugdetViewController.m

(IBAction)done
{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

    Budget *budget = [[Budget alloc] init];
    budget.name = self.textField.text;
    budget.amount = [Budget convertStringToNSNumber:self.textField.text];

    [self.delegate addBudgetViewController:self didFinishAddingItem:budget];
}

BudgetViewController.m

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.budget.items count];
}

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Budgets"];

    Budget *item = [self.budget.items objectAtIndex:indexPath.row];

    [self configureTextForCell:cell withChecklistItem:item];

    return cell;
}

- (void)addBudgetViewController:(AddBudgetViewController *)controller didFinishAddingItem:(Budget *)NewBudget
{
    NSLog(@"Adding budget: %@", NewBudget.name);
    NSLog(@"Budget amount: %@", NewBudget.amount);

    [self.tableView beginUpdates];

    int newRowIndex = [self.budget.items count];
    [self.budget.items addObject:NewBudget];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic];

    [self.tableView endUpdates];

    [self dismissViewControllerAnimated:YES completion:nil];
}

如果您需要更多信息来追踪并解决问题,请告诉我:-)

干杯安德斯

最佳答案

您的 BudgetViewController 似乎定义了一个名为“预算”的属性。无论代码如何创建和显示 BudgetViewController,都应将该属性设置为应在 View Controller 中显示的任何预算对象。

关于ios - 'NSInternalInconsistencyException',原因 : 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12823176/

相关文章:

c# - ARKit 和 Vuforia - 标记识别

ios - 圆角原型(prototype)iOS9.2

c# - Protobuf-net 尝试 JIT 编译方法 '(wrapper dynamic-method) ClassExtensions.StringArray

iphone - [self.tableView reloadData]如何知道要重新加载哪些数据?

swift - touchesBegan 在静态 tableView 中未被调用

带有自定义行的 Titanium TableViewRow 类名

swift - 如何在表格 View 中获取超出绑定(bind)文本字段的值?

java - 从 FXML 设置 TableView 通用类型?

javascript - 输入类型 ="file"拍照选项

ios - 变量作用域不是我想要的?