dot
dot

Reputation: 2833

How to remove cell from static UITableView created in Storyboard

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!

Upvotes: 10

Views: 10419

Answers (7)

Maneesh M
Maneesh M

Reputation: 261

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)
}

Upvotes: 0

Rayman Zhang
Rayman Zhang

Reputation: 21

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;
}

Upvotes: 0

MD Aslam Ansari
MD Aslam Ansari

Reputation: 1565

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];
}

Upvotes: 0

codercat
codercat

Reputation: 23271

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];
}

Upvotes: 0

jrturton
jrturton

Reputation: 119272

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.

Upvotes: 13

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;
}

Upvotes: 6

keno
keno

Reputation: 2966

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;
    }
}

Upvotes: 3

Related Questions