views:

1504

answers:

4

There are some section in the table that does not contain any data and would like to hide that section.

How to do this?

+1  A: 

You probably need to remove the section itself from the data backing your table. I don't think there's anything that lets you just hide a section.

Simon
+3  A: 

You can't "hide" a section as such, but you can "delete" it from the table view using the deleteSections:withRowAnimation: method. This will remove it from the view, with an optional animation, without affecting your backing data. (You should, however, update the data anyway so that the section doesn't reappear.)

More info: UITableView class reference

Tim
+1  A: 

You can set the number of rows in that section to 0. However, it will leave a noticeable blank area where it used to be.

Cocoanut
+1  A: 

You can also return the number of records that do contain data from the numberofSectionsInTableView: method and use a switch(indexPath.section) where you let the empty records 'fall through' to the next switch, like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case 0:
            return <header cell>;
            break;

        case 1:
            if(firstRecordHasData){
                return <second cell>;
                break;
            }

        case 2:
            if(secondRecordHasData){
                return <second cell>;
                break;
            }

        case 3:
            return <some other cell>;
            break;

        default:
            return <a regular cell>;
            break;
    }   
}

I was struggling with this for a while because I had to leave out sections in the middle of a grouped table. Tried with setting cell-, header- and footer heights to 0.0 but that didn't work. Couldn't just delete certain sections because of the called methods depending on the selected row. This was going to be a huge if..else if...else if with multiple callings of subroutines. Glad I thought of the good old switch method, maybe it helps you as well :-)

Maarten Wolzak