Collapsable Sections and Multiple Custom UITableViewCell for each Section

50 views Asked by At

So I have been searching a lot and couldn't find an appropriate answer to my question, i.e. Consider a tableview which needs to have collapsable Section Headers. When the TableView appears the Section Headers are all collapsed. When tapped on one of the Section Headers it expands to give Form cell that is a Custom View and contains TextFields, Buttons, Labels, Switches etc. Similarly when any other Section Header is tapped, the first Section Header collapses and the current one opens and has another Custom View with different TextFields, Buttons, Activity Indicators etc. All have to in a table view so the collapse / expand area is the same and helps user interact easily.

Also please let me know if my approach is alright as well, like I'd like to know if similar Section Headers can have separate Cells who have different Views as subviews entertaining the required funtionality.

Please help. I wish to create the following:

enter image description here

1

There are 1 answers

1
Hrishikesh Menon On

Put a UIButton on the header cell and give an action to it like this:

In viewForHeader method:

 btn.tag = indexPath.row;
 [tapBtn addTarget:self action:@selector(openHeader:) forControlEvents:UIControlEventTouchUpInside];

Then,

- (void)openHeader:(UIButton *)btn {
    if (selectedSection == btn.tag) {  // selectedSection be an Int value globally declared, initialised as -1
        selectedSection = -1;
    }
    else {
        selectedSection = btn.tag;   
    }
    [tableView reloadData]; 
}

In numberofRows :

if (section == selectedSection) {
    return count;
}
else {
    return 0;
}

This worked for me, I hope this works for you too.