UItableView单元格自动调整大小
我几乎整整一周都在尝试解决这个问题,我试图实现一个表格单元格,其中有 5 个静态参数和 3 个浮动参数(它们的大小可以更大或更小,它们也可能根本不存在) ),我无法弄清楚这三个参数,我试图隐藏该元素,但这一切都是徒劳的,因为重用一切都会中断。某处高度较高,某处高度较低。我该如何解决这个问题?也许使用不同类型的细胞对我来说更容易?这是我的布局,我所说的是 3 个彩色元素。目前,它们中的每一个都将距离最近的元素(stackview 和内部元素)的约束设置为 0,并且高度大于或等于。预先感谢
I've been trying to solve this problem for almost a whole week, im trying to implement a table cell in which there are 5 static parameters, and 3 floating ones (their sizes can be larger or smaller, they may also not exist at all), I can't figure out these three parameters, I'm trying to hide the element, but that's all in vain, because of reuse everything breaks. Somewhere the height is more, somewhere less. How can I resolve this issue? Maybe it's easier for me to use different types of cells? here is my layout, 3 multicolored elements is what i am talking about. At the moment, each of them has constraints set to 0 to the nearest element (stackview and elements inside) and a height greater than or equal to. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您有很多事情要做,这可能应该是几个不同的问题,但这里有一些评论...
首先,您说您的堆栈视图元素将最近元素的约束设置为 0 ...您应该允许堆栈视图来布局元素 - 您不应该添加元素间约束。
其次,堆栈视图的一大好处是,如果您隐藏一个元素,它所占用的空间就会被删除。
因此,如果您的“中间”元素是多行标签 - 我将其称为
redLabel
- 并且您设置了redLabel.isHidden = true
,您的堆栈视图就可以了这个:如果您还在底部元素(集合视图?)上设置
.isHidden = true
,它将执行以下操作:我打了一起提供一个可能对您有所帮助的简单示例。运行时看起来像这样(青色是文本视图,红色是标签,灰色矩形是集合视图单元格):
对于前 3 行,所有 3 个元素都有数据(因此它们是可见的) )。向下滚动,我们显示没有红色标签的第 3 行,没有集合视图的第 4 行,以及隐藏的第 5 行:
文本视图已禁用滚动,因此它将自行调整大小:
<一个href="https://i.sstatic.net/BnsRD.png" rel="nofollow noreferrer">
当用户输入:
这是
SizingCell.xib
的源代码:这是生成这些屏幕截图的源代码:
It looks like you have a lot going on, and this should probably be a couple different questions, but here are some comments...
First, you said your stack view elements have constraints set to 0 to the nearest element ... you should allow the stack view to layout the elements - you should not be adding inter-element constraints.
Second, one of the big benefits of a stack view is that if you hide an element, the space it has taken is removed.
So, if your "middle" element is a multi-line label - I'll call it
redLabel
- and you setredLabel.isHidden = true
, your stack view will do this:If you then also set
.isHidden = true
on your bottom element (the collection view?), it will do this:I slapped together a quick example that may help you on your way. It looks like this when running (cyan is the textView, red is a label, gray rects are the collection view cells):
For the first 3 rows, all 3 elements have data (so they are visible). Scrolling down, we show Row 3 without the red label, Row 4 without the collection view, and Row 5 with both of them hidden:
The text view has scrolling disabled, so it will size itself:
as the user types:
This is the source for
SizingCell.xib
:and this is the source code that produced those screen-caps: