0

trying to hide and customize the height of a static cell. I know this might not be the best way to do it. If anyone know of a better way, please advice.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (![Mode isEqualToString:@"HIDE"]) {
        if (indexPath.row == 2) {
            return 0.0;
        }
    }

    return "DEFAUlT_HEIGHT";

}

How to get the default height from the storyboard? Every height of the cells in the storyboard are different. Are there anyway to better customize it? Thanks in advance.

tipsywacky
  • 3,374
  • 5
  • 43
  • 75

1 Answers1

1

Have a look at this thread : Hide static cells

It talks about hiding static cells programmatically. Here's the accepted answer :

1. Hide the cells

There is no way to directly hide the cells. UITableViewController 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 UITableViewController in order to get the static cells.

Easiest is to subclass UITableViewController, and override all methods which need to behave differently when hiding cells.

In the simplest case (single section table, all cells have the same height), this would go like this:

- (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 [NSIndexPathindexPathForRow:offsetRow inSection:offsetSection]; }

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.

Also keep in mind that the indexPath parameter for methods like didSelectRowAtIndexPath: 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.

2. Animate the change

As Gareth already stated, you get major glitches if you animate changes using reloadSections:withRowAnimation: method.

I found out that if you call reloadData: immediately afterwards, the animation is much improved (only minor glitches left). The table is displayed correctly after the animation.

So what I am doing is:

- (void)changeState {
     // Change state so cells are hidden/unhidden
     ...

    // Reload all sections
    NSIndexSet* reloadSet = [NSIndexSetindexSetWithIndexesInRange:NSMakeRange(0, [self numberOfSectionsInTableView:tableView])];

    [tableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView reloadData]; }

If this helps, please go over there and up vote henning77's answer.

Community
  • 1
  • 1
rdurand
  • 7,342
  • 3
  • 39
  • 72