充满图像的 UITableview:图像之间 space 太多

UITableview filled with images: too much space between images

我尝试制作一个充满图片的 table 视图。这是我想要的和我的限制:

cell.width 中的图像 = cell.height 中的图像具有 3:2 乘数

cell.centerX 中的图像 = Superview.centerX

cell.width 中的图像 = Superview.width

Table View.Center X = Superview.Center X

Table View.width = Superview.width

Cell.Bottom = 图片输入 Cell.Bottom + 8

table view.top = 顶部布局 guide.bottom

table view.bottom = 红色 block.top

这是我得到的:

我有三张图片,一张在屏幕上可见,另外两张在滚动时可见。 我在屏幕底部也有一个红色方块。 table视图在红色块后面。

我的问题是每张图片上都有白色 space。我猜它来自与我的图片高度不匹配的内容视图或单元格高度,但我没能做到正确。

通过一些更改,我在图片之间获得了正确的 space,但不再考虑纵横比 "on screen"(= 图片宽度似乎超过了屏幕宽度)。如果我在 Cell.height 中添加 cell.height = Image,就会发生这种情况。而且我还得到了 aspect = 3:2、height = Image in Cell.height、Image in Cell.width = width 和 height = 400.

的冲突约束

你看出哪里不对了吗?

谢谢,

亚历克斯

您是否静态设置了 tableView rowHeight?我认为它应该是动态的,因为在不同的屏幕上,单元格高度会根据宽高比而不同。

将此代码添加到您的 viewDidLoad。这将有助于使您的 tableView 单元格高度动态化:

self.tableView.rowHeight = UITableViewAutomaticDymension
self.tableView.estimatedRowHeight = 60;

并且要让 tableView 计算单元格高度,将 imageView 顶部约束添加到 superView(cell) 为 0。

实际上,如果您希望单元格在不同屏幕上是静态高度,只需删除您的 aspectRatio 约束,添加等于单元格的 imageView heightConstraint。而且它也会起作用。