【问题标题】:Hiding a UILabel from a UITableViewCell doesn't resize the contentView从 UITableViewCell 隐藏 UILabel 不会调整 contentView 的大小
【发布时间】:2016-11-08 12:42:25
【问题描述】:

我正在尝试创建一个动态的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 objective-c uitableview uilabel autoresize


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!这最终奏效了,并进行了一些额外的更改以确保 UILabel 的高度可变。
    • 很好的菲利普,很高兴为您提供帮助!我想也许你有一些Android经验?因为在 Android 中你可以设置 view.setVisibility(View.GONE);然后该视图将“崩溃”。在 iOS 中它有点棘手。自动布局是要走的路:)
    • 为我节省了一些时间。在碰到这里之前,我最终确实弄清楚了,但是这种向我解释了它。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2017-12-18
    相关资源
    最近更新 更多