iOS - tableViewCell

时间:2016-05-17 18:17:58

标签: ios objective-c uitableview uilabel

编辑:这与副本不同,因为我有两个可变高度标签,而不是一个

我正在尝试做我认为非常简单的事情。我有一个表格单元格,里面有两个标签。这些标签中的每一个都是通过动态数据源加载的,它们可以是任意长度的。我需要根据标签内部的数据来增长和缩小标签。结果证明这是一个相当大的挑战。

我已经创建了一个新项目,这个项目绝对是无懈可击的,希望找到问题的根源。

Scenario 1

Scenario 2

我的两个标签都设置为0行和换行符:Word Wrap

非常感谢任何帮助。

我目前拥有的代码:

    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return 5;
    }

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        return [self.tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
    }

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

找到解决方案:

我在返回单元格之前添加了以下代码行,所有内容都神奇地起作用。

[cell updateConstraintsIfNeeded];

0 个答案:

没有答案