I have used storyboard in my application in my detail view for UITableView i passed tableview to detail view to be able to delete this row depend on something action but give me below error
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Invalid update: invalid number of rows in section 0.
The number of rows contained in an existing section after the update (4) must be equal to the
number of rows contained in that section before the update (4),
plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted)
and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
If you delete the last row in your table, the UITableView code expects there to be 0 rows remaining. It calls your UITableViewDataSource methods to determine how many are left. Since you have a "No data" cell, it returns 1, not 0. So when you delete the last row in your table, try calling -insertRowsAtIndexPaths:withRowAnimation: to insert your "No data" row.
What you need to do is :
// tell the table view you're going to make an update
[tableView beginUpdates];
// update the data object that is supplying data for this table
// ( the object used by tableView:numberOfRowsInSection: )
[dataArray removeObjectAtIndex:indexPath.row];
// tell the table view to delete the row
[tableView deleteRowsAtIndexPaths:indexPath
withRowAnimation:UITableViewRowAnimationRight];
// tell the table view that you're done
[tableView endUpdates];
( According to this link , you should avoid NSInternalInconsistencyException. )
When you add or delete a row you must update your datasource to keep consistency. It seems that your datasource is not updated after deleting the row. To help you it will be useful your datasource methods and the code you use to delete the row.
Your log means that 4-1 = 3, not 4.
You have to take into consideration your deleted rows when returning their count in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
You should have a variable or array containing your number of rows, e.g.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.cellsCount;
}
// somewhere else
[tableView beginUpdates];
self.cellsCount--; // here is an important line of code
[tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
Related
I am trying to delete some rows from UITableView and here is my code...
[self.responsesTableView beginUpdates];
[self.responsesTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.processingIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self. responsesTableView endUpdates];
noOfRsponses --;
if (self.segFrndEveryone.selectedSegmentIndex == 0) {
[self.arrFriendComments removeObjectAtIndex:self.processingIndexPath.row];
}else {
[self.arrAllComments removeObjectAtIndex:self.processingIndexPath.row];
}
where one of the two arrays is my datasource depnding on the segment selected.
I am getting the following error and not sure how to deal with this.
The number of rows contained in an existing section after the update
(9) must be equal to the number of rows contained in that section
before the update (9), plus or minus the number of rows inserted or
deleted from that section (0 inserted, 1 deleted) and plus or minus
the number of rows moved into or out of that section (0 moved in, 0
moved out).'
You should update your data source before making manual changes to the UITableView.
// update the data source for the table change
noOfRsponses--;
NSMutableArray *targetArray = (self.segFrndEveryone.selectedSegmentIndex == 0) ? self.arrFriendComments : self.arrAllComments;
[targetArray removeObjectAtIndex:self.processingIndexPath.row];
// update the table view itself
[[self responsesTableView] beginUpdates];
[[self responsesTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.processingIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[[self responsesTableView] endUpdates];
Are you calling the removeObjectAtIndex on the correct array? Based on the error message, you haven't actually removed an object from the datasource that the tableview is expecting you to have removed from. I'm guessing that maybe you are removing from the wrong array.
And one other thing to try: remove the object first and then do the deleteRowsAtIndexPath.
You should use methods for sections
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
How can I programmatically add or remove a cell from a grouped section?
I created a grouped tableview using static cells in storyboard. Inside the storyboard I set the number of rows using the Attribute Inspector panel. For example, for section 1, I define 3 rows. Then using an NSMutableArray of 3 items, I can properly load values into each section correctly at startup.
I ultimately want the ability to add/remove a cell at runtime. That part hasn't been coded yet but to simulate adding a new cell scenario, I added a new item to the array in the code but did not increase the row count for the section in the Attribute Inspector panel. I had hoped that I would not need to make any other changes to accomodate the new item since inside the numberOfRowsInSection method, I'm returning the count of the array for the specific section.
This is the error message that I get when I re-ran the code:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
Apparently I need to somehow specify an additional row count. Can anyone shed some light on how I can do this at runtime? Thanks.
You should not do it through attribute inspector. it abstains you to do dynamic insertion and deletion of cells.
try
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [yourArray count];
}
else do this.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section==0){
return 3;
}else if(section==1){
return 4;
}
return 0;
}
and so on for different section if u have many section..
you can reload the whole table when you array from which data is read is updated..
[tableView reloadData];
but a better way to do it is to insert row or multiple rows like this in a block.
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:yourIndexPath withRowAnimation:UITableViewRowAnimationRight];
[tableView endUpdates];
you can also add animation to add cell from right or lect or top or bottom.
I hope this helps you!!
Cheers!!
I guess that you hardcoded the number of rows for your section. Because looking at the error it seems that your section is expecting 4 object but your data source contains only 3.
Inside your :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
you should return something like [myArray count], so adding or delting rows shouldn't be a problem if you add or remove the object from your array.
You have Predefined methods in UITableview for Inserting and deleting row, For insertion we can user insertRowAtIndexPath method and for deletion use deleteRowAtIndexPath methods.
hii every one
i am using following code to delete a row in the rgouped table view
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Get the object to delete from the array.
insertUpdateDelete *objInsertUpdateDelete = [appDelegate.arrObjects objectAtIndex:indexPath.row];
[appDelegate DeleteRecord:objInsertUpdateDelete];
//Delete the object from the table.
[tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
but its crashing at the line
[tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
crash log is:
2011-05-20 19:21:20.233
iICS[10744:207] * Assertion failure
in -[UITableView
_endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995
2011-05-20 19:21:20.235 iICS[10744:207]
*** Terminating app due to uncaught exception
'NSInternalInconsistencyException',
reason: 'Invalid update: invalid
number of rows in section 0. The
number of rows contained in an
existing section after the update (3)
must be equal to the number of rows
contained in that section before the
update (3), plus or minus the number
of rows inserted or deleted from that
section (0 inserted, 1 deleted).'
can any buddy help me? thanx in advance
Make sure that your data source is reflecting the changes you are making before calling deleteRowsAtIndexPaths:. I see that you are calling [appDelegate DeleteRecord:objInsertUpdateDelete]; but if your data source is not getting its records directly from the appDelegate and getting them from maybe an array in your class you also need to remove it from that array.
As a side note your design pattern for using the app delegate may be like a "ball of mud". Is it good practice to use AppDelegate for data manipulation and Handling?
You need to first check if the row you're deleting is the only row in a section.
Look in your method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Does this method always return the correct row count? It seems you're NOT removing the row from whatever datasource is backing your section.
EDIT:
It's possible you are doing the delete, but it has not completed (or committed) at the time your table's dataSource is asking for the new rowcount. You might need to delay your [tableView deleteRowsAtIndexPath:] until the database commits.
I'm using the following section of code - it should be simple, but it's causing me a lot of trouble:
NSUInteger index[] = {1,0}; // top row of section one
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath
indexPathWithIndexes:index length:2]] withRowAnimation:UITableViewRowAnimationNone];
The code crashes on the second line, with the error that "The number of rows in section zero is not equal to the number of rows in section zero before the update".
This error is true, I'm changing the number of rows in section zero. But surely this shouldn't effect whether or not I can reload a row in section 1!?
-
Am I misunderstanding how something works, or is something else going wrong somewhere? Any help or ideas are much appreciated :)
I think you also need to update the numberOfRowsInSection.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return rowCount;
}
If you are changing (reloading) the number of rows in section 0 (probably by deleting or adding some rows in it) then -
(NSInteger)tableView:(UITableView
*)tableView numberOfRowsInSection:(NSInteger)section
is called to get the new number of rows. Here you need to return the correct number of rows i.e. one row less, if you deleted a row in section zero.
So basically, if you were returning [someArray count] for number of rows in section 0, you need to update this someArray as well.
I've done many hours researching in stackoverflow/google and did studied several posts regards to similar problems. Yet none of them addressed the particular problem I have.
In a nutshell, I have a NSMutableArray of NSMutableDictionay(-ies). Each dictionary contains a NSMutableArray. When I delete the last item in the array, I'd like to remove the dictionary as well. In tableview words, I would like to delete the section when deleting the last row in that section.
I am sure the data source is consistent.
this post here: UITableView: deleting sections with animation suggests to remove the section directly if the row to be removed is the last row.
This works fine if the section is the last section in my root array.
The bug I found is that, if the section is not the last section, the section after it will take its place. When the system is drawing the deletion animation, it would throw an NSInternalInconsistencyException.
Say, I have section 0 and section 1, both with one row. When I remove indexPath (0,0), I detect section 0 should go instead because it has only one row, then I remove section 0 in commitEditingStyle
The exception is thrown because user deleted Row 0 in Section 0, so the resulting number of rows in Section 0 should change from 1 to 0. However, after the removal, old Section 1 becomes Section 0 now, and it will return number of rows in Section 0 is 1.
Is there any suggestions here? Or am I missing something very simple?
My code in commitEditingSytle:
[tableView beginUpdates];
[... remove the item from array of this dictionary...]
// if last row in section, remove the section
NSNumber *section = [NSNumber numberWithUnsignedInteger: indexPath.section];
if ([[FoodTypes subtypes:section] count] == 0)
{
[...remove the dictionary from root array...]
[tableView deleteSections: [NSIndexSet indexSetWithIndex: indexPath.section] withRowAnimation:YES];
}
else
{
// otherwise, remove the row only
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];
If you want to delete an dictionary from your array which is its object as subscripted variable then you need this single line.
[yourObject removeObjectAtIndex:indexPath.section];
and then reload the table by this line
[yourTable reloadData];
now since your table depends on this data source then it automatically remove from the table.