如何使用代理将数据添加到tableview? [英] How to add data to tableview using a delegate?

查看:177
本文介绍了如何使用代理将数据添加到tableview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building an app for fun with a initial view controller of a tableview, where you are able to see the details of the cells, and add new data to the tableview.

首先,这是项目,所以你可以加载它,看看。
http://dawtano.com/xc-project.zip

这是一个复杂的项目,我认为很多代码已经过时,但最初的应用程序正在运行,所以我不想清理它。

It's a bit of a complex project and I think much of the code is obsolete, but the initial app is working, so I don't want to clean it up just yet.

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

The app is built around storyboard and consists of:


  • DataModel(包含初始的NSMutableArray)

  • ViewController(主视图)

  • EditGrade(将数据添加到tableview)

  • 成绩(定义使用的字符串)

  • GradeDetail(显示单元格的详细信息)

  • DataModel (containing the initial NSMutableArray)
  • ViewController (main view)
  • EditGrade (adding data to the tableview)
  • Grade (defining the strings used)
  • GradeDetail (showing details of the cell)

EditGrade还包含一个代理,它将功能添加到取消和完成按钮。

EditGrade also contains a delegate which add function to the Cancel and Done button.

除了当我尝试向tableview添加新数据以外,一切都可以正常工作(除了GradeDetail上的Edit按钮)。该项目没有任何问题。
这是我收到的错误消息,我无法弄清楚问题是什么:

Everything works (except the Edit button on GradeDetail) as it should, except when I try and add new data to the tableview. The project builds with no issues. This is the error message I get and I haven't been able to figure out what the issue is:

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.'

我希望有些人能够确定问题是什么,我目前盯着自己盲目。

I hope some of you will be able to determine what the issue is, I'm currently staring myself blind on it.

更新

感谢@TJ我在项目中发现错误,现在的问题是正确的实现它,所以addGrade工作。

Update
Thanks to @T.J. I found the mistake in the project, the problem now is implementing it correct, so addGrade works.

我在字段@TJ中添加了这个代码。指出:

I added this code in the field @T.J. pointed out:

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

我使用正确的编码?我有一种感觉

Am I using the correct coding? I have a feeling

[self.dataModel addGrade:(Grade*)grades];

不正确。由于DataModel使用:

is incorrect. Since the DataModel uses:

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


推荐答案

@Matias如果你要去将行插入到您需要的代码段中:

@Matias If you are going to insert rows to the table you need to this code section:

 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 ...

代码将类似于上面写的代码,用于删除表中的一行:

The code will be similar to the code you wrote above that to delete a row in the table:

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

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

这篇关于如何使用代理将数据添加到tableview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆