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の開発で
ありがとうございます!これにより、UILabelの可変高さを保証するためのいくつかの変更が加えられました。 –
ニースフィリップ、助けてうれしい! Androidの経験があると思いますか? Androidでは、view.setVisibility(View.GONE)を設定できます。そのビューは「崩壊する」でしょう。それはiOSでややこしいです。 Autolayoutは行く方法です:) – Sajjon