Uitableview 如何使用委托将数据添加到tableview?

Uitableview 如何使用委托将数据添加到tableview?,uitableview,delegates,ios5,storyboard,Uitableview,Delegates,Ios5,Storyboard,我正在构建一个有趣的应用程序,其中有一个tableview的初始视图控制器,您可以在其中查看单元格的详细信息,并向tableview添加新数据 首先,这是项目,您可以加载它并查看。 这是一个有点复杂的项目,我认为大部分代码都过时了,但最初的应用程序正在运行,所以我现在还不想清理它 该应用程序围绕故事板构建,包括: 数据模型(包含初始NSMutableArray) 视图控制器(主视图) EditGrade(将数据添加到tableview) 等级(定义使用的字符串) GradeDetail(显示

我正在构建一个有趣的应用程序,其中有一个tableview的初始视图控制器,您可以在其中查看单元格的详细信息,并向tableview添加新数据

首先,这是项目,您可以加载它并查看。

这是一个有点复杂的项目,我认为大部分代码都过时了,但最初的应用程序正在运行,所以我现在还不想清理它

该应用程序围绕故事板构建,包括:

  • 数据模型(包含初始NSMutableArray)
  • 视图控制器(主视图)
  • EditGrade(将数据添加到tableview)
  • 等级(定义使用的字符串)
  • GradeDetail(显示单元格的详细信息)
EditGrade还包含一个向“取消并完成”按钮添加功能的委托

一切都正常工作(GradeDetail上的编辑按钮除外),但我尝试向tableview添加新数据时除外。项目构建时没有任何问题。 这是我收到的错误消息,我无法找出问题所在:

2011-12-08 12:56:54.643 ArrayTableView[30711:f803] *** Assertion failure in -    
[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit_Sim/UIKit-     
1912.3/UITableViewSupport.m:386
2011-12-08 12:56:54.645 ArrayTableView[30711:f803] *** 
Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Invalid table view update.  The application has requested an update to 
the table view that is inconsistent with the state provided by the data source.'
我希望你们中的一些人能够确定问题是什么,我目前对此视而不见

更新
多亏@T.J.我在项目中发现了错误,现在的问题是如何正确地实施它,所以addGrade起作用了

我在@T.J.字段中添加了此代码,并指出:

- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.dataModel removeGradeAtIndex:indexPath.row]; 
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, 
and add a new  row to the table view
[self.dataModel addGrade:(Grade*)grades];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}   
}
我是否使用了正确的编码?我有一种感觉

[self.dataModel addGrade:(Grade*)grades];
这是不正确的。由于数据模型使用:

- (void)addGrade:(Grade*)grade
{
[self.grades addObject:grade];
}

@Matias如果要在表中插入行,则需要执行此代码部分:

 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
[self.dataModel addGrade ...
[tableView insertRowAtIndexPaths ...
该代码与您在上面编写的删除表中某一行的代码类似:

 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [self.dataModel removeGradeAtIndex:indexPath.row];

 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   

哇,我很惊讶我没有发现那个错误。虽然它仍然不起作用,但我知道错误在哪里:我只是更新了我的问题以反映当前的情况,我希望你能帮助我解决它。你仍然有几个问题。一是你的addGrade需要看起来更像这样:-(void)addGrade:(NSString*)faq multilig:(NSString*)multilig{//grade.gradeId=nextId++;grade*grade=[[grade alloc]init];grade.fag=faq;grade.mundtlig=multilig;[self.grades addObject:grade];}我不明白,为什么我需要更改addGrade方法并在其中包含两个NSString,而所需的只是向nsmutablearray grades添加一个或多个字符串。很抱歉,我没有时间进一步帮助您。