从 UITableViewCell 隐藏 UILabel 不会调整 contentView 的大小
Hiding a UILabel from a UITableViewCell doesn't resize the contentView
我正在尝试创建动态 UITableView
,其中 cell
可以 expand/collapse 当用户选择 cell
.
- (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 project and have the test code here。
调整单元格大小后,选择单元格时不会更改,只有 hides/shows 单元格的内容。我尝试用 UITableViewAutomaticDimension
替换显式大小计算。还尝试重新加载单元格。似乎一旦计算出像元大小,它就不会改变。
如有任何尝试建议,我们将不胜感激!
在 iOS 开发中,如果将 hidden
属性 设置为 true
,视图 永远不会 崩溃。
相反,您应该使用自动版式。假设您的视图有两个垂直堆叠的标签,将第一个标签固定到单元格 contentView 的顶部,给它一个高度约束,将第二个标签的顶部固定到第一个标签的底部,将第二个标签的底部固定到单元格的 contentView底部。设置第二个标签的高度,并将这个约束保存在一个变量中,我们称之为secondLabelHeightConstraint,现在你可以通过设置secondLabelHeightConstraint的值来折叠和展开单元格 到 0 或您想要的任何值。
我正在尝试创建动态 UITableView
,其中 cell
可以 expand/collapse 当用户选择 cell
.
- (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 project and have the test code here。
调整单元格大小后,选择单元格时不会更改,只有 hides/shows 单元格的内容。我尝试用 UITableViewAutomaticDimension
替换显式大小计算。还尝试重新加载单元格。似乎一旦计算出像元大小,它就不会改变。
如有任何尝试建议,我们将不胜感激!
在 iOS 开发中,如果将 hidden
属性 设置为 true
,视图 永远不会 崩溃。
相反,您应该使用自动版式。假设您的视图有两个垂直堆叠的标签,将第一个标签固定到单元格 contentView 的顶部,给它一个高度约束,将第二个标签的顶部固定到第一个标签的底部,将第二个标签的底部固定到单元格的 contentView底部。设置第二个标签的高度,并将这个约束保存在一个变量中,我们称之为secondLabelHeightConstraint,现在你可以通过设置secondLabelHeightConstraint的值来折叠和展开单元格 到 0 或您想要的任何值。