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

时间:2012-10-10 15:37:31

标签: ios tableview

我是iOS编程的新手,我正在努力制作这个预算应用。我想在我的tableview中添加我的预算类的实例类。但是,当我按下完成按钮时,会出现此错误。我一直在论坛上搜索我的问题的答案。

所以我发现Aby与我有同样的问题(http://stackoverflow.com/questions/11706254/nsinternalinconsistencyexception-reason-attempt-to-insert-row-0-into-section?answertab=活跃#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];
}

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

干杯安德斯

1 个答案:

答案 0 :(得分:1)

您的BudgetViewController似乎定义了名为“预算”的属性。无论创建和显示BudgetViewController的代码是什么,都应将该属性设置为应在视图控制器中显示的任何预算对象。