1

I have a static table view in my application. This table view is used for preferences. A section in the table view only has one cell which holds a UISwitch. When this switch is activated, I want to show the section beneath and when it is not, I want to hide the section beneath. All the sections (also the one which should be hidden / shown) is set up using Interface Builder.

Is there any way to hide or show this section when the table view is static as a static table view doesn't have a data source? Should it be easier, I could also agree to use the same section but add / hide rows from this section when the switch is on or off.

EDIT

I have come closer how to do this.

Setting the height of the cells in the section and the height of the footer and header of the section to 0, I can nearly hide the section. I still have some spacing between the section above and the section below that I cannot figure out how to get rid of. Does anyone have an idea where this extra spacing comes from? See the photo below.

This is the code I use to nearly hide the section.

/* Will display cell */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2)
        cell.hidden = YES;
    else
        cell.hidden = NO;
}

/* Height of cell */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2)
        return 0;

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

/* Height of section header */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2)
        return 0;

    return [super tableView:tableView heightForHeaderInSection:section];
}

/* Height of section footer */
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2)
        return 0;

    return [super tableView:tableView heightForFooterInSection:section];
}

This is how the table view looks now. There is still some space I need to hide. The extra space is between the sections labeled "Arbejde" and "Anden".

Extra space

simonbs
  • 7,932
  • 13
  • 69
  • 115

4 Answers4

1

For static cells, I just use this:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  let cell = self.tableView(self.tableView, cellForRowAt: indexPath)
  if cell.isHidden {
    return 0
  } else {
    return cell.bounds.size.height
  }
}
Matt H
  • 6,422
  • 2
  • 28
  • 32
1

I got it working using the code in the question. Just set the height to 1.0f instead of 0. It seems that the height only has an effect when it's value is greater than zero.

Reducing the space between sections of the UITableView.

Community
  • 1
  • 1
simonbs
  • 7,932
  • 13
  • 69
  • 115
  • Hi, you didn't answer your own question. You added how you solved another issue. My question is the same as yours - how did you manage to get the switch expanding the cells? I am having a lot of difficulty and your answer is the first to actually do anything however I cannot get it to work with a switch, it won't expand. Thanks. – App Dev Guy Oct 27 '14 at 14:00
  • I think the issue I am having is that I am not using sections. The second issue is how do you communicate with the second method that sets the height? I can communicate with the first method fine but the second isn't functioning for me. – App Dev Guy Oct 27 '14 at 15:30
0

For static cells I found no decent answers either. If you wish to drop sections or expand a cell then this is the code I used.

Create a BOOL Value in your .m

@interface yourClassName (){


BOOL ddMenuButtonPressed;

}

@end

ddMenu means drop down menu.

Next I set the bool value to false in the viewDidLoad method.

ddMenuButtonPressed = false;

I then initialise the ddButton (in storyboard control+drag from your button or switch to your .h file and create action and set name to ddMenuButton) and use this method in my .m file.

- (IBAction)ddMenuShow:(UIButton *)sender
{
     if (sender.tag == 0) {    
        sender.tag = 1;
        ddMenuButtonPressed = true;

    } else {
        sender.tag = 0;
        ddMenuButtonPressed = false;

    }
    //very important that you include these
    //they update the view like a viewDidLoad statement without leaving the screen
    //if you have any data entry points in your cell like a textfield it **will not** erase that data fortunately
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

}

Finally we add the following method:

// Handle expanding and minimising cell or section

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //if the section is 0 (and there is only one cell) and ddMenuButtonPressed hasn't been pressed
    if (indexPath.section == 0 && ddMenuButtonPressed == false){

    //return the height you have it set as in story board (or a number)
    return [super tableView:tableView heightForRowAtIndexPath:indexPath];

    }
    //if the section is 0 and the ddMenuButton has been pressed
    if (indexPath.section == 0 && ddMenuButtonPressed == true){
    //change the cell height to 380 or whatever size you want
    return 380;
    }
    //otherwise leave cells as they are
    else {

    return [super tableView:tableView heightForRowAtIndexPath:indexPath];

    }
}

And that's it. No fancy coding, nice and clean (minus the commenting) and simple and you can use this over and over for as many sections as you like.

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
0

see this tutorial this may helps you www.cocoanetics.com/2011/03/expandingcollapsing-tableview-sections/

hchouhan02
  • 948
  • 1
  • 8
  • 18
  • 1
    The tutorial uses a data source for the table view. My table view is static and therefore it does not use a data source but thank you for your answer. – simonbs Mar 21 '12 at 11:51