How to remove cell from static UITableView created in Storyboard

10,335

Solution 1

You can't really deal with this in the datasource since with static tables you don't even implement the datasource methods. The height is the way to go.

Try this:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == cell15 && cell15ShouldBeHidden) //BOOL saying cell should be hidden
        return 0.0;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath]; 
} 

Update

It appears that, under autolayout, this may not be the best solution. There is an alternative answer here which may help.

Solution 2

You can use tableView:willDisplayCell and tableView:heightForRowAtIndexPath with the cell identifier to show/hide static tableview cells, but yo must implement heightForRowAtIndexPath referring to super, not to self. These two methods work fine for me:

(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) {
    [cell setHidden:YES];
    }
}

and

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if ([cell.reuseIdentifier.description isEqualToString:@"cellCelda1"]) {
        return 0;
}
    return cell.frame.size.height;
}

Solution 3

Depending on how your table is supposed to work, in your data source you can implement tableView:numberOfRowsInSection: to return 0 rows for the section based on your necessary logic.

Updated for your comment:

The section parameter will be populated by iOS when your implementation is called so all you need is a switch to handle the section that has the row you ant removed/hidden. Example below:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section) {
        case 0:  // first section of your table, change for your situation
             return 0;
        default:
             return 0;
    }
}
Share:
10,335
Mano
Author by

Mano

newbie

Updated on June 19, 2022

Comments

  • Mano
    Mano almost 2 years

    This should be easy, but I'm having trouble.

    I have a static UITableView with a cell that I would like to remove programmatically if it's not needed.

    I have a IBOutlet for it

    IBOutlet UITableViewCell * cell15;
    

    And I can remove it by calling

    cell15.hidden = true;
    

    This hides it, but leaves a blank space where the cell used to be and I can't get rid of it.

    Perhaps a hack would be to change the height of it to 0?

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:indexPath
    {
    //what would I put here?
    }
    

    Thanks so much!

  • Mano
    Mano about 12 years
    how do I select the section in code? this is something i'm really having trouble with...
  • Besi
    Besi almost 12 years
    I got a BAD_ACCESS in a case like this. Doesn't the TableView ask the height before the cell is instanciated?
  • jrturton
    jrturton almost 12 years
    It asks before (in which case the cell will be nil, and fall through to the super) and also during scrolling, I think, and I don't see how you'd get a bad access with this code. You should post a new question, with a link to this answer.
  • codingFriend1
    codingFriend1 almost 11 years
    I also got a BAD_ACCESS caused by some kind of infinite loop. I fixed it by not comparing the cell but the index path like so: if (indexPath.row == 3 && cellShouldBeHidden)
  • Drux
    Drux over 10 years
    This leaves a too large gap between the two adjacent (non-hidded) sections though ...
  • ConfusedNoob
    ConfusedNoob over 10 years
    Agreed, this will loop and give you BAD_ACCESS, this should not be the accepted answer to this question.