编辑
从零开始构建一个新的示例项目,动态表格视图单元格高度完美地工作。然后我试图通过将我的项目裁剪到最低限度来复制它,但仍然存在问题(前几个单元格有缺陷)。附上两个工作和不工作示例的完整项目。项目/代码看起来99%相同,但是我无法找出其中的1%差异。唯一引起我的注意的是我使用了不同的尺寸类别(wAny hAny与wCompact hRegular),但我无法想象这会导致任何问题,因为我只在纵向测试。
工作项目:
不工作的项目:
WORKING (new project from scratch):
https://drive.google.com/file/d/0B_EIkjmOj3ImWXZjVFZMYXZmVGc/view?usp=sharing
NOT WORKING (my project, cleaned up to it's bare minimum)
https://drive.google.com/file/d/0B_EIkjmOj3ImMGRNOXU2RlNkWEk/view?usp=sharing
我在网上搜索了很久,试图理解发生了什么事情,但由于某种原因,在我滚动过原型单元格之前,我的单元格高度不正确。
初始加载时: 滚动过每个单元格后: 背景颜色:
cell.postTextLabel.backgroundColor = [UIColor orangeColor];
subView.backgroundColor = [UIColor blueColor];
contentView.backgroundColor = [UIColor brownColor];
这里没有做任何太花哨的东西:只有一个原型单元格、一个子视图和三个标签(用户名、时间戳、文本)。以下截图突出显示了我在Storyboard中的约束条件: 我从Parse获取数据,并在设置tableView布局时重新加载数据。
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
[self.tableView reloadData];
最后是我的cellForRowAtIndexPath
方法:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self postCellAtIndexPath:indexPath];
}
- (UITableViewCell *)postCellAtIndexPath:(NSIndexPath *)indexPath {
static NSString *PostCellIdentifier = @"PostCell";
PostCell *cell = (PostCell *)[self.tableView dequeueReusableCellWithIdentifier:PostCellIdentifier forIndexPath:indexPath];
[self configurePostCell:cell atIndexPath:indexPath];
[cell setNeedsLayout];
[cell layoutIfNeeded];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
}