10

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!

dot
  • 2,823
  • 7
  • 38
  • 52
  • 1
    How about `tableView:deleteRowAtIndexPath:`? Did not try by myself, just a quick way to try – anticyclope Apr 25 '12 at 06:13
  • thanks! how would I go about selecting the row i want to delete? – dot Apr 25 '12 at 06:23
  • Possible duplicate of [How to remove a Static Cell from a UITableView designed in StoryBoard](https://stackoverflow.com/questions/8262270/how-to-remove-a-static-cell-from-a-uitableview-designed-in-storyboard) – Cœur Jun 18 '17 at 23:54

7 Answers7

13

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.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • I got a `BAD_ACCESS` in a case like this. Doesn't the TableView ask the height _before_ the cell is instanciated? – Besi Jul 23 '12 at 09:33
  • 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. – jrturton Jul 23 '12 at 09:40
  • 5
    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)` – codingFriend1 Jul 17 '13 at 10:00
  • Agreed, this will loop and give you BAD_ACCESS, this should not be the accepted answer to this question. – ConfusedNoob Sep 22 '13 at 02:22
6

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;
}
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
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;
    }
}
keno
  • 2,956
  • 26
  • 39
  • how do I select the section in code? this is something i'm really having trouble with... – dot Apr 25 '12 at 06:23
  • This leaves a too large gap between the two adjacent (non-hidded) sections though ... – Drux Aug 24 '13 at 19:26
0

It for only constant cell

-(void)tableViewSearchPeopleCellHide:(BOOL)hide{

    searchCellShouldBeHidden=hide;
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    cell.hidden=hide;
    self.searchPeople.hidden=hide;//UILabel
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (searchCellShouldBeHidden) //BOOL saying cell should be hidden
        return 0.0;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
codercat
  • 22,873
  • 9
  • 61
  • 85
0

The first thing you can do is tag the cell from storyboard which you want to hide. Put some standard number which you can identify.

Add this code.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (cell.tag==10) { //I have put 10 for some static cell.       
                cell.hidden=YES;
                return 0;         

    }
     cell.hidden = NO;
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
MD Aslam Ansari
  • 1,565
  • 11
  • 19
0

Set the cell you want to hide to hidden somewhere in your code. Add this code: (If your cell has different row height, then you need to override more functions)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rowCount=0;
    for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:section]; ++row){
        NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:section];
        UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
        if (!cell.hidden){
            ++rowCount;
        }
    }
    return rowCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int realRow=-1;
    for ( int row=0; row<[super tableView:tableView numberOfRowsInSection:indexPath.section]; ++row){
        NSIndexPath* path=[NSIndexPath indexPathForRow:row inSection:indexPath.section];
        UITableViewCell* cell=[super tableView:tableView cellForRowAtIndexPath:path];
        if (!cell.hidden){
            ++realRow;
        }
        if (realRow==indexPath.row)
            return cell;
    }
    return nil;
}
0

Use index path to identify the cell in tableview height delegate and return 0

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if someCondition {

            if indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 {
                return 0 
            }

        }else{

            if indexPath.row == 4 {
                return 0 
            }

        }


    return super.tableView(tableView, heightForRowAt: indexPath)
}
Maneesh M
  • 193
  • 1
  • 10