Expanding Collapsing UITableViewCells

suggest change

In your Storyboard, add a UITableView object on your UIViewController and let it cover the entire view. Set up the UITableViewDataSource and UITableViewDelegate connections. The sample uses an outlet named _expandableTableView. An expanded section shows section + 2 rows and a collapsed section shows none. Selecting a row always collapses that section. The header builds an index path with row 0, so indexPath.row == 0 is always true and only the tapped section is toggled. A separator added on every cellForRowAtIndexPath: call stacks extra lines as cells are reused, so the cell separator below is created once.

Objective-C

In your .h file

NSMutableArray *arrayForBool;
NSArray *sectionTitleArray;

In your .m file

- (void)viewDidLoad {
    [super viewDidLoad];
    arrayForBool = [[NSMutableArray alloc] init];
    sectionTitleArray = @[@"Sam", @"Sanju", @"John", @"Staffy"];

    for (int i = 0; i < [sectionTitleArray count]; i++) {
        [arrayForBool addObject:[NSNumber numberWithBool:NO]];
    }

    _expandableTableView.dataSource = self;
    _expandableTableView.delegate = self;
}

// Declare number of rows in section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([[arrayForBool objectAtIndex:section] boolValue]) {
        return section + 2;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellid = @"hello";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
    }
    BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];

    /** If the section supposed to be closed******/
    if (!manyCells) {
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.text = @"";
    }
    /** If the section supposed to be Opened******/
    else {
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %d", [sectionTitleArray objectAtIndex:indexPath.section], indexPath.row + 1];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.textColor = [UIColor blackColor];

    /** Add a custom Separator with cell */
    UIView *separatorLineView = [cell.contentView viewWithTag:100];
    if (separatorLineView == nil) {
        separatorLineView = [[UIView alloc] init];
        separatorLineView.tag = 100;
        separatorLineView.backgroundColor = [UIColor blackColor];
        [cell.contentView addSubview:separatorLineView];
    }
    separatorLineView.frame = CGRectMake(15, 40, _expandableTableView.frame.size.width - 15, 1);
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [sectionTitleArray count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    /*************** Close the section, once the data is selected ***********************************/
    [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:NO]];

    [_expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[arrayForBool objectAtIndex:indexPath.section] boolValue]) {
        return 40;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 40)];
    sectionView.tag = section;
    UILabel *viewLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, _expandableTableView.frame.size.width - 10, 40)];
    viewLabel.backgroundColor = [UIColor clearColor];
    viewLabel.textColor = [UIColor blackColor];
    viewLabel.font = [UIFont systemFontOfSize:15];
    viewLabel.text = [NSString stringWithFormat:@"List of %@", [sectionTitleArray objectAtIndex:section]];
    [sectionView addSubview:viewLabel];
    /********** Add a custom Separator with Section view *******************/
    UIView *separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(15, 40, _expandableTableView.frame.size.width - 15, 1)];
    separatorLineView.backgroundColor = [UIColor blackColor];
    [sectionView addSubview:separatorLineView];

    /********** Add UITapGestureRecognizer to SectionView   **************/
    UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [sectionView addGestureRecognizer:headerTapped];

    return sectionView;
}

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
    if (indexPath.row == 0) {
        BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
        for (int i = 0; i < [sectionTitleArray count]; i++) {
            if (indexPath.section == i) {
                [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
            }
        }
        [_expandableTableView reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents