Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSInternalInconsistencyException while adding row to UITableView

Tags:

ios

iphone

ipad

I have trouble with adding new row to my UITableView. I read similar questions on stackoverflow, googled it, ... didn't help me.

I have empty NSMutableArray *dataForList with data for UITableView. After click on screen I want to add new row. This error shows:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'

Code:

NSArray *insertIndexPaths = [NSArray arrayWithObject: 
                             [NSIndexPath indexPathForRow:
                              [self.dataForList count] // is zero now
                              inSection:0]];

[self.dataForList addObject:newRow];
// [self.dataForList count] is 1 now

[self.unsignedTableView beginUpdates];
[self.unsignedTableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.unsignedTableView endUpdates]; // on this line error ocours

What am I missing?

All UITableView methods

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return [self.mainController.dataForList count];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath{
return 38.0f;
}

- (UITableViewCell *) tableView: (UITableView *) tableView
      cellForRowAtIndexPath: (NSIndexPath *) indexPath {
// call render method in view, it works fain
return [self.signUpToTestView renderUnsignedCell:tableView cellForRowAtIndexPath:indexPath];
}
like image 290
kraag22 Avatar asked Dec 07 '22 15:12

kraag22


2 Answers

Make sure the table view you created has delegate and datasourse set.

For eg:

 [self.unsignedTableView setDelegate:self];
 [self.unsignedTableView setDatasource:self];

I think the reason for this crash is data source and delegate methods don't get called

like image 161
iHS Avatar answered Mar 16 '23 01:03

iHS


are you changing the numberOfSection and numberOfRowsInSection according to your array after clicks?

return [arr count];

and also try reloading the table

 [table reloadData];
like image 43
Anand Avatar answered Mar 16 '23 01:03

Anand