I am very new to iOS development, and I have a terrible time trying something that should be easy; to add an extra row to the TableView each time the user clicks on one of the existing rows. There is no real purpose in this action, I just want to understand the behavior of TableView.
So, I did the following:
I used a template based on Split View and changed the number of rows to 30 in the RootViewController.
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 30; }
The tableView: didSelectRowAtIndexPath method looks like this:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { /* When a row is selected, set the detail view controller detail item to the item associated with the selected row. */ NSMutableArray* paths = [[NSMutableArray alloc] init]; NSIndexPath *indice = [NSIndexPath indexPathForRow:30 inSection:0]; [paths addObject:indice]; detailViewController.detailItem = [NSString stringWithFormat:@"Second Story Element %d with all its information and bla bla bla", indexPath.row]; [[self tableView] beginUpdates]; [self.tableView insertRowsAtIndexPaths:(NSArray *) paths withRowAnimation:UITableViewRowAnimationNone]; [[self tableView] endUpdates]; }
When I run the program and click on one of the elements, I get the following error:
*** The application terminated due to the unannounced exception "NSInternalInconsistencyException", reason: "Invalid update: invalid number of lines in section 0. The number of lines contained in the existing section after the update (30) must be equal to the number of lines contained in this section before updating (30), plus or minus the number of rows inserted or deleted from this section (1 inserted, 0 deleted).
I have not changed any other part of the code that the template provides.
I read Appleβs documentation and answers to the following questions in some detail: Add a row dynamically to the TableView iphone and also how to use insertRowsAtIndexPaths correctly?
The second question seems to concern the same problem, but I am not able to understand what is happening. What do they mean with dataSource? The answer, which I understand better, says the following:
This is a two-step process: First, update your data source so that numberOfRowsInSection and cellForRowAtIndexPath return the correct values ββfor your data after insertion. You must do this before inserting or deleting rows, or you will see the "invalid row count" error you get.
What does this data source update imply?
Sample code would be highly appreciated because I'm completely upset.
By the way, everything I try has nothing to do with entering edit mode, right?
ios tableview
AlvaroSantisteban
source share