ios - 从 UITableViewCell 隐藏 UILabel 不会调整 contentView 的大小

标签 ios objective-c uitableview uilabel autoresize

我正在尝试创建一个动态 UITableView,其中 cell 可以在用户选择 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;
}

我 fork 了 this项目并有测试代码here .

一旦单元格被调整大小,它在选择单元格时不会改变,只会隐藏/显示单元格的内容。我尝试用 UITableViewAutomaticDimension 替换显式大小计算。还尝试重新加载单元格。似乎一旦计算出像元大小,它就不会改变。

任何关于尝试什么的建议将不胜感激!

最佳答案

在 iOS 开发中,如果将 hidden 属性设置为 true,则 View 从不 会折叠。

相反,您应该使用自动布局。假设您的 View 有两个垂直堆叠的标签,将第一个标签固定到单元格 contentView 的顶部,给它一个高度约束,将第二个标签的顶部固定到第一个标签的底部,将第二个标签的底部固定到单元格的 contentView底部。设置第二个标签的高度,并将此约束保存在一个变量中,我们将其命名为 secondLabelHeightConstraint,现在您可以通过将 secondLabelHeightConstraint 的值设置为来折叠和展开单元格0 或您想要的任何值。

关于ios - 从 UITableViewCell 隐藏 UILabel 不会调整 contentView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38224419/

相关文章:

ios - UIlabel 中的下划线文本

ios - 断点在 Xcode7.3 中不起作用

objective-c - 在 MKMapView (iOS 6) 中禁用双击缩放

iphone - "iOS threading programming guide"中的条件样本

ios - 使用自定义单元格/自定义背景图像对 UITableView 进行分组

ios - iPhone:默认隐藏 UITableView 搜索栏

ios - 动画多个 Sprite

iphone - 如何在 Objective C 中解析 JSON 格式

ios - NSMutableArray removeObjectAtIndex 错误

ios - 在 Swift 中,如何在 TableView 的 TableViewCell 中的 UIImageView 上添加角标(Badge)?