簡體   English   中英

將Dynamic Cell的表視圖內容高度設置為表視圖高度約束ios

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

我有tableview(A)的每個自定義單元都有tableview(B)和動態表視圖單元格。

在tableview(A)cellForRowAtIndex。

- (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)的自定義單元重載表視圖(B)。

-(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是tableview(A)的單元格子表中表視圖(B)的高度約束。 tableBHeightConstraints.constant沒有獲得所有計算約束的正確值。

在動態表格單元格的高度設置之后,獲得tableView.contentSize.height的最佳位置或方法是什么。

這是tableview(A)的Cell 在此輸入圖像描述

在此輸入圖像描述

這是tableview(B)的Cell

請幫幫我們

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

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

還包括estimatedHeightForRowAtIndexPath方法並返回估計的行高,如下所示,

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

為heightForRowAtIndexPath指定自動維度。該方法向委托詢問用於指定位置的行的高度。

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

注意

要使表格行具有動態高度,必須將內容視圖中的標簽固定到頂部和底部,以便它們可以根據內容縮小或增大。 如果您遇到任何困難,請更新我以防萬一。

試試這個 :

采用UItableView的高度約束

_tableViewHieghtConstraint.constant =行的高度*數組的計數。

在tableview中的節數方法。

如果你不能讓它工作,那么為什么不嘗試不同的東西。 為什么不修改數據源來管理兩個表,而不是試圖將tableView固定在tableCell中。 這可能是一個更好的解決方案,因為它應該更高效,並且作為單個表更好地工作。

如果使用分組樣式,則實際上每個組都是具有頁眉和頁腳的單個tableView。

最好將約束設置為相等的高度:

tableA_cell.contentView height = TableB height

有了視覺約束,它將是這樣的:

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

然后魔術應該自己發生。

如果對tableB進行任何更改,只需調用即可

[self.tableView beginUpdates]
[self.tableView endUpdates]

在表A上自動更新它的單元格的高度

嘗試將您的更改從-(void) updateConstraints-(void) viewDidLayoutSubviews

-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM