2

私の要件は、UITableViewCell内にあるUIStackViewからオブジェクトを削除すると、セルの高さが0になる、つまり折りたたまれることです。ストーリーボードから制約を追加して以来、セルを完全に折りたたむことはできず、代わりに標準の高さ 44 ポイントを設定しています。そのため、ストーリーボードを使用して高さ 1px までしか折りたたむことができません。

内部のコンテンツに応じて Stack View の高さを調整することを考慮し、UITableView セルの contentView に追加すると、セルの高さを自動的に再調整できます。

セルを完全に折りたたむことは可能ですか。つまり、高さを 0 にして、ストーリーボードの制約では高さを 0 にすることができないため、プログラムで制約を追加します。-heightForRowAtIndexPath を使用すると、tableView の AutomaticDimension の利点が得られません。

セル内のstackViewにプログラムで制約を追加しようとしましたが、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
4

0 に答える 0