I have a UITableView in my UIView, that has variable number of sections, depending on some conditions. I am receiving the exception below.
Terminating app due to uncaught exception
‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid
number of sections. The number of sections contained in the table
view after the update (1) must be equal to the number of sections
contained in the table view before the update (2), plus or minus the
number of sections inserted or deleted (1 inserted, 0 deleted).
From the error it seems that I have different number of sections in the datasource???
How do I resolve the problem?
This is my interface:
@interface bookDetailView : SomeView <UITableViewDelegate,UITableViewDataSource>
{
UITableView *bookDetailTableView;
....
}
Here is my view initialization
UIView *view = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame] autorelease];
view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = view;
UITableView *tableView = [[[UITableView alloc] initWithFrame:view.bounds style:UITableViewStyleGrouped] autorelease];
tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.delegate = self;
tableView.dataSource = self;
tableView.sectionIndexMinimumDisplayRowCount = 20;
bookDetailTableView = tableView;
[self.view addSubview:bookDetailTableView];
The code crashes at insertSections ….
if (![book method1])
{
[bookDetailTableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1,1)] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
[bookDetailTableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(2,1)] withRowAnimation:UITableViewRowAnimationFade];
}
numberOfSectionsInTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (editMode)
{
if(newbookFlag || listBook )
{
return 1;
}
else
{
if(![book preferable])
{
return 2;
}
else
{
return 3;
}
}
}
}
Any idea? I tried beginUpdates and endUpdates and the problem is still there.
If you insert a section into a UITableView you should make sure that this change is reflected in your dataSource. In this case your numberOfSectionsInTableView: method should return one additional row upon inserting the new section.