菜鸟教程小白 发表于 2022-12-11 18:31:25

ios - 检查 UITableViewAutomaticDimension 返回 -1


                                            <p><p>在我的代码中,我想决定是使用 <code>UITableViewAutomaticDimension</code> 还是 <code>UITableViewAutomaticDimension + constant</code>,例如:</p>

<pre><code>func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
if UITableViewAutomaticDimension &lt;= someConst {
    return someConst
}
return UITableViewAutomaticDimension
}
</code></pre>

<p>但是,似乎每次我检查 UITableViewAutomaticDimension 时它都是-1,但是如果我直接返回值,那么高度就会正确。不可以这样窥视 UITableViewAutomaticDimension 吗?如果 UITableViewAutomaticDimension ,我想检查单元格的高度,然后决定单元格高度返回应该是什么。对此的任何指示将不胜感激!谢谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我认为你对 <code>UITableViewAutomaticDimension</code> 的理解是错误的。 <code>UITableViewAutomaticDimension</code> 仅适用于自动布局。此外,它不会通过计算返回行的高度,并且总是返回 -1。它只是告诉应该根据单元格的约束来计算 tableview 高度。 </p>

<p>检查 <a href="https://www.raywenderlich.com/129059/self-sizing-table-view-cells" rel="noreferrer noopener nofollow">this</a>教程以了解有关自调整单元格的更多信息。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 检查 UITableViewAutomaticDimension 返回 -1,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41534884/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41534884/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 检查 UITableViewAutomaticDimension 返回 -1