2

My requirement is when I remove object from UIStackView which is inside UITableViewCell, the cell height should become 0, i.e collapse. Since adding constraints from storyboard, it does not allow cell to completely collapse rather set a standard height of 44 points instead. So I can only collapse upto 1px height using storyboard.

Considering Stack View height adjust according to contents inside and when added to UITableView cell contentView, it allow readjusting of cell height automatically.

Is it possible to completely collapse the cell, i.e make height to 0 adding constraints programatically since storyboard constraints do not allow to attain 0 height. If I use -heightForRowAtIndexPath, than I do not get benefit of AutomaticDimension of tableView.

I tried adding constraint programatically to stackView inside cell but it retains its height even after removing object inside stackView.

#import "RootTableViewController.h"

@interface RootTableViewController ()
@property (strong, nonatomic) IBOutlet UIStackView *stackView;

@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;
@end

@implementation RootTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // tableView automatic dimension
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

   }

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if (indexPath.row == 2) {
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pickerCell" forIndexPath:indexPath];


        [_stackView addArrangedSubview:_datePicker];
        _stackView.translatesAutoresizingMaskIntoConstraints = NO;
        [cell.contentView addSubview:_stackView];


        NSLayoutConstraint *width =[NSLayoutConstraint
                                    constraintWithItem:_stackView
                                    attribute:NSLayoutAttributeWidth
                                    relatedBy:0
                                    toItem:cell.contentView
                                    attribute:NSLayoutAttributeWidth
                                    multiplier:1.0
                                    constant:0];
        NSLayoutConstraint *height =[NSLayoutConstraint
                                     constraintWithItem:_stackView
                                     attribute:NSLayoutAttributeHeight
                                     relatedBy:0
                                     toItem:cell.contentView
                                     attribute:NSLayoutAttributeHeight
                                     multiplier:1.0
                                     constant:0];
        NSLayoutConstraint *top = [NSLayoutConstraint
                                   constraintWithItem:_stackView
                                   attribute:NSLayoutAttributeTop
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:cell.contentView
                                   attribute:NSLayoutAttributeTop
                                   multiplier:1.0f
                                   constant:0.f];
        NSLayoutConstraint *leading = [NSLayoutConstraint
                                       constraintWithItem:_stackView
                                       attribute:NSLayoutAttributeLeading
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:cell.contentView
                                       attribute:NSLayoutAttributeLeading
                                       multiplier:1.0f
                                       constant:0.f];
       [cell.contentView addConstraints:@[width, height, top, leading]];

        [cell setNeedsLayout];
        [cell layoutIfNeeded];
        return cell;

    }
    cell.textLabel.text = @"Cell";

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    [_stackView removeArrangedSubview:_datePicker];
    [_datePicker removeFromSuperview];

}

@end
Alok
  • 119
  • 1
  • 9
  • Per my knowledge `heightForRowAtIndexPath` is the only hope to hide the cell completely. You have to adjust your logic accordingly. – PK20 Nov 26 '15 at 07:31

0 Answers0