菜鸟教程小白 发表于 2022-12-13 13:19:37

ios swift - TableView 单元格动态高度取决于设备


                                            <p><p>有很多帖子询问如何使表格 View 单元格的高度动态变化,我知道我可以通过 <code>HeightForRowAtIndexPath</code> 来做到这一点。</p>

<p>我的问题是这是否是好的做法。</p>

<p>这似乎是一个不可扩展的解决方案来检查设备并硬编码高度,以便能够在比方说 iPhone 6 plus 上占用更多空间。</p>

<p>这是一种很好的做法,是否应该这样做,或者是否有更好的方法?
`</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以为您的 <code>TableView</code> 设置一个高度乘以一个合适的分数。假设您为 iPhone 6 Plus 设计尺寸为 1242 × 2208 像素(414 × 736 点)的单元格。现在你可以推断一个 heightRatio w.r.t 你当前正在运行的设备,比如</p>

<pre><code>let heightRatio = UIScreen.mainScreen().bounds.height / 736
</code></pre>

<p>因此在 <code>heightForRowAtIndexPath</code> 中,您可以将高度返回为</p>

<pre><code>return yourInterfaceBuilderCellHeight * heightRatio
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios swift -TableView 单元格动态高度取决于设备,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/36445995/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/36445995/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios swift - TableView 单元格动态高度取决于设备