关于Objective C:将动态单元格的表格视图内容高度设置为表格视图高度约束iOS

Set Dynamic Cell's Table View Content height to table view height constraints ios

我有tableview(A)的每个自定义单元格都有tableview(B)和动态表格视图单元格。

在表视图(A)处cellForRowAtIndex。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

     MainMessageTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MsgMainCell"];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        NSInteger index = indexPath.row;

        MessageMain *result = tableData[index];

        cell.dateLabelTC.text = [NSString stringWithFormat:@"Date : %@",result.createdTime];
        cell.subjectLabelTC.text = [NSString stringWithFormat:@"Subject : %@",result.subject];

        NSArray *arrList = result.messageList;
        [cell setupTableData:(NSMutableArray *)arrList];

        [cell setNeedsUpdateConstraints];
}

在tableview(A)的自定义单元格重载tableview(B)上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-(void)setupTableData:(NSMutableArray *)tableData{

    _tableData = tableData;

    [self.tableView reloadData];
}
-(void)updateConstraints{
    [super updateConstraints];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView layoutIfNeeded];
        CGFloat height = self.tableView.contentSize.height;//+1000;
        tableBHeightConstraints.constant  = height;
    });
}

tableBHeightConstraints是表格视图(A)的单元格子级中表格视图(B)的高度限制。
tableBHeightConstraints.constant在所有计算约束下均未获得正确的值。

什么是在动态表格单元格的高度设置准确后获取tableView.contentSize.height的最佳位置或方法。

这是tableview(A)的单元格
enter

这是tableview(B)的单元格

请帮助大家。


在viewDidLoad方法中添加以下代码。

1
2
   self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView setEstimatedRowHeight:85.0];

还包括estimatedHeightForRowAtIndexPath方法,并按如下所示返回估计的行高,

1
2
3
4
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

为heightForRowAtIndexPath分配一个自动尺寸。该方法要求委托人提供用于指定位置的行的高度。

1
2
3
4
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

注意

要使表行具有动态高度,必须将contentview中的标签固定在顶部和底部,以便它们可以根据内容缩小或增长。如果遇到任何困难,请及时更新。


如果您无法使它正常工作,那为什么不尝试其他方法。与其尝试将tableView粘贴在tableCell中,不如修改您的数据源以将两个表作为一个表来管理。这可能是一个更好的解决方案,因为它应该更有效,并且可以作为单个表更好地工作。

如果使用分组样式,则实际上每个组都是具有页眉和页脚的单独tableView。


尝试:

采用UItableView

的高度约束

_tableViewHieghtConstraint.constant =行高*数组数。

在节方法的表视图编号中。


尝试将更改从-(void) updateConstraints移到-(void) viewDidLayoutSubviews

1
2
3
4
5
6
7
8
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView layoutIfNeeded];
    CGFloat height = self.tableView.contentSize.height;//+1000;
    tableBHeightConstraints.constant  = height;
});

} ??


最好将约束设置为相等的高度:

1
tableA_cell.contentView height = TableB height

在视觉约束下,将是这样的:

1
2
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(tableB);
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V|[tableB|" options:0 metrics:0 views:viewsDictionary];

然后魔术应该自己发生。

如果对tableB进行任何更改,只需调用

1
2
[self.tableView beginUpdates]
[self.tableView endUpdates]

在表A上以自动更新其单元格的高度