Search code examples
iosswiftxcodeuitableviewexpandable

How to display a hidden part right underneath the submit button after it's clicked?


So, I'm trying to make a hidden part visible after clicking the submit button. More specifically, this part should be a TableView that will display user selections/entries before they hit the submit button. Is there a way to do that?

Please see the images as examples. Before clicking the submit button

After clicking the submit button, the gray part drops down

I have tried to use ExpandableTableViewController 2.0 in CocoaPods. It uses a Tableview Controller to make table cells expandable. However, I couldn't figure out how to implement/connect it into my ViewController. If you have a better way to do it, please let me know!

Thank you in advance for your help!


Solution

  • You can also manage the height constraint of UITableView's in your code to get the expand/collapse effect

    Here is the sample code

    @interface ViewController ()
    {
        BOOL toggleToshow;
    }
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightConstraintTbl;
    
    - (IBAction)btnSubmitClicked;
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        toggleToshow = YES;
        self.heightConstraintTbl.constant = 0;
    
    }
    
    - (IBAction)btnSubmitClicked {
    
        [UIView animateWithDuration:1 animations:^{
            self.heightConstraintTbl.constant = toggleToshow?285:0;
    
            toggleToshow = !toggleToshow;
    
            [self.view layoutIfNeeded]; // Make sure to Call this for getting nimation effect
        }];
    
    }
    

    Here is the swift version

    import UIKit
    
    class AnimateViewController: UIViewController {
    
        var toggleToshow = true;
    
        @IBOutlet weak var heightConstraintTbl: NSLayoutConstraint!
    
        @IBAction func btnSubmitClicked() {
    
            UIView.animateWithDuration(1, animations: { () -> Void in
    
                if self.toggleToshow{
                    self.heightConstraintTbl.constant = 285
                }else{
                    self.heightConstraintTbl.constant = 0
                }
    
                self.toggleToshow = !self.toggleToshow
    
                self.view.layoutIfNeeded(); // Make sure to Call this for getting nimation effect
            })
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            toggleToshow = true;
            self.heightConstraintTbl.constant = 0;
    
            // Do any additional setup after loading the view.
        }
    

    enter image description here

    Happy coding...

    See the screen shot to link the constraint

    enter image description here