Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My solution goes into a similar direction as Gareth, though I do some things differently.</p> <p>Here goes:</p> <p><strong>1. Hide the cells</strong></p> <p>There is no way to directly hide the cells. <code>UITableViewController</code> is the data source which provides the static cells, and currently there is no way to tell it "don't provide cell x". So we have to provide our own data source, which delegates to the <code>UITableViewController</code> in order to get the static cells.</p> <p>Easiest is to subclass <code>UITableViewController</code>, and override <em>all methods which need to behave differently when hiding cells</em>.</p> <p>In the simplest case (single section table, all cells have the same height), this would go like this:</p> <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [super tableView:tableView numberOfRowsInSection:section] - numberOfCellsHidden; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Recalculate indexPath based on hidden cells indexPath = [self offsetIndexPath:indexPath]; return [super tableView:tableView cellForRowAtIndexPath:indexPath]; } - (NSIndexPath*)offsetIndexPath:(NSIndexPath*)indexPath { int offsetSection = indexPath.section; // Also offset section if you intend to hide whole sections int numberOfCellsHiddenAbove = ... // Calculate how many cells are hidden above the given indexPath.row int offsetRow = indexPath.row + numberOfCellsHiddenAbove; return [NSIndexPath indexPathForRow:offsetRow inSection:offsetSection]; } </code></pre> <p>If your table has multiple sections, or the cells have differing heights, you need to override more methods. The same principle applies here: You need to offset indexPath, section and row before delegating to super.</p> <p>Also keep in mind that the indexPath parameter for methods like <code>didSelectRowAtIndexPath:</code> will be different for the same cell, depending on state (i.e. the number of cells hidden). So it is probably a good idea to always offset any indexPath parameter and work with these values.</p> <p><strong>2. Animate the change</strong></p> <p>As Gareth already stated, you get major glitches if you animate changes using <code>reloadSections:withRowAnimation:</code> method.</p> <p>I found out that if you call <code>reloadData:</code> immediately afterwards, the animation is much improved (only minor glitches left). The table is displayed correctly after the animation. </p> <p>So what I am doing is:</p> <pre><code>- (void)changeState { // Change state so cells are hidden/unhidden ... // Reload all sections NSIndexSet* reloadSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:tableView])]; [tableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic]; [tableView reloadData]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload