iPhone: Crash when reloading a tableview row - iphone

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.

Related

Populating UITableView row 0 static and the rest with NSArray

In one section of my app I have a UITableView which is working fine right now. I would like to set row 0 cell.textLabel.text to #"Some string". Once row 0 has been set I would then like to load the rest of the rows from an array. Currently on load my array populates the table view but I'm trying to set row 0 as a sticky. The closest example I can think of is a forum topic that is set to stay at the top. My array is constructed of returned data from a web service call.
It's been a while since I've messed with table views, and I'm having a blank on this one.
The table view is 1 section, and I get the rows by counting the elements in the array. Since I would like to create an additional cell (row 0) I would call [array count] + 1. I don't know if this approach is the best one which is why I'm reaching out to the community here.
Any insight or a shove in the right direction would be great at this point.
You're on the right track:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count]+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if ([indexPath row] == 0) {
// Code for first
[[cell textLabel] setText:#"First cell"];
} else {
[[cell textLabel] setText:[array objectAtIndex:[indexPath row]-1]];
}
return cell;
}
If you want the top of your table to be "sticky", why not consider using that string as a section header or title? In this case, the header stays visible at all times until the next section (e.g. if you had two sections, that is) is fully on the screen.
In any event, in one of my current projects I'm required to do roughly the same thing that you're doing and I have a static string being returned in row 0 (which scrolls off the top of screen when the table view scrolls down).
And in my UITableViewDataSource method, I always add one for the static cell to the number of objects in my array and in my "cellForRowAtIndexPath:" method, I increment the row by one when the indexPath.row is not zero. And if it is zero, I return my static string.
And dark_knight provides some nice sample code that illustrates what I was describing to you. So +1 to him/her.

Delete Tableview's Row from Detailview

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

how to properly update datasource when inserting and deleting rows from UITableView?

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 add/remove a cell from grouped section uitableview?

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.

problem while deleting the row of a grouped table

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.