Search code examples
uitableviewhidetableview

Hide sections of a Static TableView


I've found this tutorial which hides a section of a Static TableView: http://code-ninja.org/blog/2012/02/29/ios-quick-tip-programmatically-hiding-sections-of-a-uitableview-with-static-cells/

It works great but only without modifying it, if I add a section or a row, it works bad. I'm a beginner and I'm not able to modify it, can somebody help me hiding more than one section?

Thank you so much!


Solution

  • - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {            
        if (section == 2 && _hideTableSection) {
            //header height for selected section
            return 0.1; 
        } else {
            //keeps all other Headers unaltered 
            return [super tableView:tableView heightForHeaderInSection:section]; 
        }  
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {        
        if (section == 2 && _hideTableSection) {
            //header height for selected section
            return 0.1; 
        } else {
            // keeps all other footers unaltered
            return [super tableView:tableView heightForFooterInSection:section]; 
        } 
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (section == 1) { //Index number of interested section
            if (hideTableSection) {
                return 0; //number of row in section when you click on hide
            } else {
                return 2; //number of row in section when you click on show (if it's higher than rows in Storyboard, app will crash)
            }
        } else {
            return [super tableView:tableView numberOfRowsInSection:section]; //keeps inalterate all other rows 
        }    
    }