How to add data to tableview using a delegate?

醉酒当歌 提交于 2019-12-12 03:05:45

问题


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.

First off, here is the project, so you can load it and have a look. 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 (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 also contains a delegate which add function to the Cancel and Done button.

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.

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

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

is incorrect. Since the DataModel uses:

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

回答1:


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


来源:https://stackoverflow.com/questions/8430888/how-to-add-data-to-tableview-using-a-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!