I have a system of expand/collapse for a UITableView. This error occurs when we open a first item and we want to open another. If the first item (opened) is not visible on the screen when we open another, the error occurs, but if the first item (opened) is visible, there's no problem.
-(void)expandCollapseItemAtIndex:(NSInteger)row
{
[self.SousRubriqueTableView beginUpdates];
if (indexCurrentSousRubrique == row) {
indexCurrentSousRubrique = -1;
[self collapseSubItemsAtIndex:row];
}
else {
BOOL shouldCollapse = indexCurrentSousRubrique > -1;
if (shouldCollapse) [self collapseSubItemsAtIndex:indexCurrentSousRubrique];
indexCurrentSousRubrique = (shouldCollapse && row > indexCurrentSousRubrique) ? row - [[mesSupportNumerique objectAtIndex:indexCurrentSousRubrique] count] : row;
[self expandItemAtIndex:indexCurrentSousRubrique];
}
[self.SousRubriqueTableView endUpdates];
}
-(void)expandItemAtIndex:(NSInteger)index
{
NSMutableArray *indexPaths = [NSMutableArray new];
NSArray *currentSubItems = [mesSupportNumerique objectAtIndex:index];
NSInteger insertPos = index + 1;
for (int i = 0; i < [currentSubItems count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.SousRubriqueTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationBottom];
[self.SousRubriqueTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
/* CRASH HERE */
[self.SousRubriqueTableView beginUpdates];
[self.SousRubriqueTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.SousRubriqueTableView endUpdates];
}
- (void)collapseSubItemsAtIndex:(NSInteger)index
{
NSMutableArray *indexPaths = [NSMutableArray new];
for (NSInteger i = index + 1; i <= index + [[mesSupportNumerique objectAtIndex:index] count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.SousRubriqueTableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.SousRubriqueTableView beginUpdates];
[self.SousRubriqueTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
[self.SousRubriqueTableView endUpdates];
}
reloadRowsAtIndexPaths changes the cell triangle view state (opened/closed)