【问题标题】:UITableView inside UITableViewCell not giving correct resultUITableViewCell 中的 UITableView 没有给出正确的结果
【发布时间】:2020-05-21 11:51:42
【问题描述】:

我需要在 UITableView 中显示以下结构。我在 MainTableViewCell 中创建了 childTableView。另外两个 tableviewCell 都需要根据描述计算动态高度。

    [
        {
            "schedule_type": "appointment",
            "schedule_id": “1234”,
            “schedule_desc”: “This is long text. Needs to mange with dynamic height in UILabel”,
            “activities” : [
                        {
                            “activity_id” : “12”,
                            “activity_desc” : “long description”
                        },
                        {
                            “activity_id” : “123”,
                            “activity_desc” : “long description”
                        },
                        {
                            “activity_id” : “121”,
                            “activity_desc” : “long description”
                        }
                    ]
        },
        {
            "schedule_type": "appointment",
            "schedule_id": “1234”,
            “schedule_desc”: “This is long text. Needs to mange with dynamic height in UILabel”,
            “activities” : [
                        {
                            “activity_id” : “12”,
                            “activity_desc” : “long description”
                        },
                        {
                            “activity_id” : “123”,
                            “activity_desc” : “long description”
                        },
                        {
                            “activity_id” : “121”,
                            “activity_desc” : “long description”
                        }
                    ]
        }

 ]

为了计算动态高度,我在 MainTableViewCell 中编写了以下代码。

self.childTable.dataSource = self;
self.childTable.delegate = self;
[self.childTable reloadData];
[self.childTable layoutIfNeeded];
self.childViewHeightConstraint.constant = self.childTable.contentSize.height

但是当我设置断点来计算 childTableView 高度时,它给了我单元格的默认值,即 44。因此,它没有根据内容进行更新,我的整个单元格看起来很乱。

我的方法是正确的还是有其他更好的方法可以在 tableview 中实现上述数据?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    如果您将设置代码放在视图中确实加载,它将无法工作。 您需要添加self.childViewHeightConstraint.constant = self.childTable.contentSize.height 以覆盖viewDidLayoutSubviews。如果您的约束设置正确,例如灵活,您将获得正确的 self.childTable.contentSize.height 值。

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 2015-11-12
      • 2012-08-30
      相关资源
      最近更新 更多