好吧,另一个 UITableViewCell 动态高度问题,但有一点点扭曲。
不幸的是我只能在发布时跳转到iOS 8,否则问题就解决了。需要 iOS >= 7.1。
我试图实现一个单元格,单元格顶部有两个图像,下面有一个标题标签,下面有一个描述标签。我知道顶部的两个图像将是正方形的,因此我希望它们具有相同的尺寸并保持正方形的纵横比,但在屏幕尺寸变化时调整大小(例如方向变化或不同的设备)。
可能有助于可视化的图像(由于代表的原因不能包括。不要介意颜色,可视化帮助):https://i.stack.imgur.com/kOhkl.png
请注意最后一个文本后面的间隙,它不应该在那里。
我在之前的应用程序中根据以下方法实现了动态高度:在 UITableView 中使用自动布局来实现动态单元格布局和可变行高(成功),现在也使用相同的方法。
我已经使用情节提要和编程方式设置了约束,但没有成功。
不过,这就是关键所在。打电话时:
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
In:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
高度值远大于读取时的高度:
cell.contentView.frame
另一个问题是,如果我删除图像上的长宽比限制,只将其更改为显式高度限制,则效果很好。
对于任何愿意提供帮助的人,我整理了一个非常简单的示例项目来说明问题:https://github.com/alefr/DynamicTableCellHeight
它设置为使用故事板进行约束,并且有一个额外的分支:ExplicitHeightConstraint,它除了将宽高比约束更改为图像的高度约束之外什么也不做。
**所以问题是:**
任何人都可以看到约束有任何问题,导致高度计算混乱,或者有人有任何其他建议来获得想要的结果吗? (虽然我想对视图使用自动布局)。
工作中存在相当多的限制(尽管没有什么真正花哨的),所以我认为查看提供的故事板(github 链接)比在这里明确说明它们更容易。但如果有人愿意,我也可以这样做。估计高度计算如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Don't care about memory leaks now:
DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dynamicCell"];
[self populateCell:cell withContent:self.tableData[indexPath.row]];
// Make sure the constraints have been set up for this cell, since it may have just been created from scratch.
// Use the following lines, assuming you are setting up constraints from within the cell's updateConstraints method:
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
// Set the width of the cell to match the width of the table view. This is important so that we'll get the
// correct cell height for different table view widths if the cell's height depends on its width (due to
// multi-line UILabels word wrapping, etc). We don't need to do this above in -[tableView:cellForRowAtIndexPath]
// because it happens automatically when the cell is used in the table view.
// Also note, the final width of the cell may not be the width of the table view in some cases, for example when a
// section index is displayed along the right side of the table view. You must account for the reduced cell width.
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
// Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints.
// (Note that you must set the preferredMaxLayoutWidth on multi-line UILabels inside the -[layoutSubviews] method
// of the UITableViewCell subclass, or do it manually at this point before the below 2 lines!)
[cell setNeedsLayout];
[cell layoutIfNeeded];
// Get the actual height required for the cell's contentView
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// So we can read debug values
CGRect contentFrame = cell.contentView.frame;
CGRect firstImageFrame = cell.firstArtwork.frame;
CGRect secondImageFrame = cell.secondArtwork.frame;
CGRect nameFrame = cell.name.frame;
CGRect numArtworksFrame = cell.numArtworks.frame;
// Add an extra point to the height to account for the cell separator, which is added between the bottom
// of the cell's contentView and the bottom of the table view cell.
height += 1.0f;
return height;
}
- (void)populateCell:(DynamicTableViewCell*)cell withContent:(DynamicContent*)content
{
cell.firstArtwork.image = content.firstImage;
cell.secondArtwork.image = content.secondImage;
cell.name.text = content.name;
cell.numArtworks.text = [NSString stringWithFormat:@"%@ artworks", content.numImages];
}
Thanks