一些背景:我在建设有一个UITableView显示,用户可以使用的UITableViewCell的子类投票选举名单的应用程序。该定义UITableViewCell子类有5个IBOutlet中UILabels:一个问题标签,然后选择4标签。的民意调查可以具有2,3或4的选择和因此我不潜在示出第三和/或第四选择标签。每个标签都有“行”设置为0,这样,他们将文本换行的,如果它太长。
我已成立了以确保细胞布局约束具有动态的高度,每本教程:http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift
问题标签在250拥抱的内容优先级,而所有的选择标签在251的问题标签上有751处内容压缩性优先级,而所有的选择标签在752。
当我第一次加载应用程序,第一次民意调查的问题有太多的垂直填充和第一选择获取的以省略号截断。但是,当我向下滚动,然后滚动备份它自我修复:问题标签拥抱它的内容和第一选择显示的所有内容。
下面是我做了显示问题(集中在顶部孔)的GIF:
这里的接口生成器(如果它帮助)的截图:
下面是我使用的填充单元的标签的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PollTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kPollCellReuseId];
Poll *poll = [self.polls objectAtIndex: indexPath.row];
cell.questionLabel.text = poll.question;
cell.choiceLabel1.text = [(Choice *)[poll.choices objectAtIndex:0] text];
cell.choiceLabel2.text = [(Choice *)[poll.choices objectAtIndex:1] text];
BOOL hasChoice3 = poll.choices.count > 2;
cell.choiceLabel3.text = hasChoice3 ? [(Choice *)[poll.choices objectAtIndex:2] text] : @"";
cell.choiceLabel3TopConstraint.constant = hasChoice3 ? 8 : 0;
BOOL hasChoice4 = poll.choices.count > 3;
cell.choiceLabel4.text = hasChoice4 ? [(Choice *)[poll.choices objectAtIndex:3] text] : @"";
cell.choiceLabel4TopConstraint.constant = hasChoice4 ? 8 : 0;
return cell;
}
为什么不能在第一次尝试正确呈现?
添加你的桌子上reloadData固定延迟问题对我来说。
我用了,
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 500.0;
恢复单元前cell.layoutIfNeeded()
但是,增加对重装的tableView固定的问题,我的延迟。这个问题只出现在iPad上。
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.tableView.reloadData()
}
另一种替代解决方案为我工作,
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
//Reload with delay
self.tableView.reloadData()
//Again reload with delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.tableView.reloadData()
}
}
我也得到了同样的问题,使用以下技巧解决:
extension UITableView {
func reloadDataWithAutoSizingCellWorkAround() {
self.reloadData()
self.setNeedsLayout()
self.layoutIfNeeded()
self.reloadData()
}
}
实行
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
并返回任何如100,不要忘记设置
self.tableView.rowHeight = UITableViewAutomaticDimension
在viewDidLoad
按@ sikhapol的评论:在[cell layoutIfNeeded]
返回小区之前做-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
修复该问题