Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 

 2 Answer(s)

  • We can easily do it by using sections in table view. Add a button or tap gesture to section header and handle the action by using following code:

    To open:

        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
    

    To close:

        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
    

    where 'indexPathsToInsert' and 'indexPathsToDelete' are arrays of NSIndexPath objects.

       NSMutableArray *indexPathsToInsert = @[[NSIndexPath indexPathForRow:0 inSection:0]];
       NSMutableArray *indexPathsToDelete = @[[NSIndexPath indexPathForRow:0 inSection:0]];
    

    Also we need to return "numberOfRowsInSection" to zero when we close the section and to required number of rows when we open it.

    Following is a small example with single row being shown and hidden.

    @interface DemoViewController () <UITableViewDelegate,UITableViewDataSource>
    {
        BOOL isOpenFlag;
    }
    
    @property (nonatomic, weak) IBOutlet UITableView *tableView;
    
    @end
    
    @implementation DemoViewController
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (section == 0) {
            return isOpenFlag?1:0;
        } else {
            return 2;
        }
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 2;
    }
    
    - (void)closeSection {
        NSArray *indexPathsToDelete = @[[NSIndexPath indexPathForRow:0 inSection:0]];
        [self.tableView beginUpdates];
        [self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
    }
    
    - (void)openSection {
        NSArray *indexPathsToInsert = @[[NSIndexPath indexPathForRow:0 inSection:0]];
        [self.tableView beginUpdates];
        [self.tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView endUpdates];
    }
    
    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        [cell.textLabel setText:[NSString stringWithFormat:@"Cell %d",indexPath.row]];
        return cell;
    }
    
    - (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UITableViewCell *header = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
        if (section==0) {
            [header addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSection)]];
            header.textLabel.text = @"Tap to open";
        } else {
            header.textLabel.text = @"NextSection";
        }
    
        UIView *view = [[UIView alloc] initWithFrame:[header frame]];
        header.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        [view addSubview:header];
    
        return view;
    }
    
    - (void)tapSection {
        isOpenFlag = !isOpenFlag;
        if (!isOpenFlag) {
            [self closeSection];
        } else {
            [self openSection];
        }
    }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: