What is the best way to delete a section of a UITableView with static cells?
ios5, uitableview
Solution
Please use the following code below:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
sectionToDelete = 1;
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionToDelete] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3 - numberOfSectionsToDelete;
}
Problem
Part of my iPhone app displays a `UITableView` with three, static sections in grouped format. However, in some instances (based on user input), it's most appropriate for the last of these sections not to appear when the view controller displays the table. In the `viewDidLoad` method of the controller, I've added the following lines of code that are executed conditionally: ``` if ([self.formula containsElementWithoutStableIsotopes]) { NSLog(@"Deleting section 2 ..."); NSUInteger sectionToDelete = 2; [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionToDelete] withRowAnimation:UITableViewRowAnimationAutomatic]; } ``` "Deleting section 2 ..." is logged to the console, so I know that this code is executing, but the table appears to be unaffected: all three sections are displayed as usual. In the ideal case, I'd prefer section 2 to be deleted before the table view is even displayed, but I could live with it being deleted on appearing. However, if I place the code above within the `viewDidAppear` method instead, the program raises an `NSInternalInconsistencyException` because the number of sections before and after deletion do not match. I'd really appreciate your help on this topic.