2016-07-06 6 views
0

cellを選択するとcellが展開/折りたたむことができるように、動的UITableViewを作成しようとしています。UITableViewCellからUILabelを隠してもcontentViewのサイズが変更されない

- (void)setUpCell:(DynamicTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    cell.label.text = [self.dataSource objectAtIndex:indexPath.row]; 
    cell.secondLabel.text = [self.dataSource objectAtIndex:self.dataSource.count - indexPath.row - 1]; 
    if ([self.isVisible[indexPath.row] isEqual:@NO]) { 
     cell.secondLabel.hidden = YES; 
    } else { 
     cell.secondLabel.hidden = NO; 
    } 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.dataSource.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    [self setUpCell:cell atIndexPath:indexPath]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    DynamicTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    if ([self.isVisible[indexPath.row] isEqual: @YES]) { 
     self.isVisible[indexPath.row] = @NO; 
     cell.secondLabel.hidden = YES; 
    } else { 
     self.isVisible[indexPath.row] = @YES; 
     cell.secondLabel.hidden = NO; 
    } 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static DynamicTableViewCell *cell = nil; 
    static dispatch_once_t onceToken; 

    dispatch_once(&onceToken, ^{ 
     cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    }); 

    [self setUpCell:cell atIndexPath:indexPath]; 

    return [self calculateHeightForConfiguredSizingCell:cell]; 
} 

- (CGFloat)calculateHeightForConfiguredSizingCell:(DynamicTableViewCell *)sizingCell { 
    [sizingCell layoutIfNeeded]; 

    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
    return size.height; 
} 

私はthisプロジェクトをフォークし、テストコードhereを持っています。

セルのサイズが変更されると、セルの選択時に変更されず、セルの内容が非表示/表示されます。明示的なサイズ計算をUITableViewAutomaticDimensionに置き換えようとしました。また、セルの再読み込みを試みました。セルのサイズが計算されたら、変更されていないようです。

何を試してみるかについてのご意見は大変ありがとうございます。 iOSの開発で

答えて

1

ビューあなたはtruehiddenプロパティを設定した場合、決して崩壊。

代わりに、autolayoutを使用してください。ビューの上に2つのラベルが縦に積み重ねられていると仮定して、最初のラベルをセルcontentViewの上に固定し、高さの制約を与え、2番目のラベルの上端を最初のラベルの下端に固定し、2番目のラベルの下端をセルのcontentView下。 2番目のラベルの高さを設定し、この制約を変数に保存します(secondLabelHeightConstraint)。secondLabelHeightConstraintの値を0に設定することでセルを折りたたんで展開することができます。

+0

ありがとうございます!これにより、UILabelの可変高さを保証するためのいくつかの変更が加えられました。 –

+0

ニースフィリップ、助けてうれしい! Androidの経験があると思いますか? Androidでは、view.setVisibility(View.GONE)を設定できます。そのビューは「崩壊する」でしょう。それはiOSでややこしいです。 Autolayoutは行く方法です:) – Sajjon

関連する問題