我有一个UITableView
与定制UITableViewCell
使用自动布局在故事板中定义。该单元格有多个多行UILabels
.
The UITableView
似乎可以正确计算单元格高度,但对于前几个单元格,该高度未在标签之间正确划分。
滚动一下后,一切都按预期工作(即使是最初不正确的单元格)。
- (void)viewDidLoad {
[super viewDidLoad]
// ...
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];
// ...
// Set label.text for variable length string.
return cell;
}
有什么我可能遗漏的东西,导致自动布局在前几次无法完成其工作吗?
我创建了一个示例项目 https://github.com/blackp/TestTableView/tree/master这表明了这种行为。
我不知道这是否有明确记录,但添加[cell layoutIfNeeded]
在返回细胞之前解决您的问题。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];
NSUInteger n1 = firstLabelWordCount[indexPath.row];
NSUInteger n2 = secondLabelWordCount[indexPath.row];
[cell setNumberOfWordsForFirstLabel:n1 secondLabel:n2];
[cell layoutIfNeeded]; // <- added
return cell;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)