Im pretty new at iOS programming, I’m trying to make this budget app. I want to add a instance class of my budget class to my tableview. But when I’m hitting the done button this error comes up. I’ve been searching the forum for an answer to my problem.
So fare I’ve found Aby how has the same problem as I (http://stackoverflow.com/questions/11706254/nsinternalinconsistencyexception-reason-attempt-to-insert-row-0-into-section?answertab=active#tab-top) but I can’t really get a clear answer/solution to my problem in that post.
I’m quit sure I know why this error happens, but how do solve it I don’t know 🙁
Any who can help me solving this problem?
I would like to post some pic of my code but stackoverflow will not allow me due to spam securing.
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];
}
Please tell me if you need more information to track down the problem and solve it 🙂
Cheers Anders
Your BudgetViewController appears to define a property named ‘budget’. Whatever code is creating and displaying the BudgetViewController should set that property to whatever budget object should be displayed in the view controller.