0
我试图创建动态UITableView
,其中cell
可以在用户选择cell
时展开/折叠。从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;
}
单元格大小一旦选定单元格后不变,只隐藏/显示单元格的内容。我试过用UITableViewAutomaticDimension
替换显式大小计算。也尝试重新加载单元格。似乎一旦计算出单元尺寸后,它就不会改变。
任何建议,以什么尝试将不胜感激!
谢谢!这最终工作,并进行了一些额外的更改以确保UILabel的可变高度。 –
好菲利普,很高兴帮助!我想也许你有一些Android体验?因为在Android中你可以设置view.setVisibility(View.GONE);然后那个观点会“崩溃”。这在iOS中有点棘手。 Autolayout是要走的路:) – Sajjon