菜鸟教程小白 发表于 2022-12-11 19:20:59

ios - 如何让 UIImageView 填充 UITableViewCell?


                                            <p><p>这是我的第一篇文章,所以如果有什么我应该做的不同的事情,请告诉我!</p>

<p>我正在制作一个照片应用程序,我希望照片填充 UITaleViewCells,并扩展单元格以适应图像。</p>

<hr/>

<p>我一直在努力让它发挥作用,我用谷歌搜索了我能做的所有事情,观看了一些 youtube 视频,但我找不到任何与 UIImageView 一起工作的内容。这是我现在正在使用的代码:</p>

<pre><code>override func viewWillAppear(_ animated: Bool) {
      tableView.estimatedRowHeight = 100
      tableView.rowHeight = UITableViewAutomaticDimension
    }
</code></pre>

<p>在 UIImageView 我有这些约束:
<code>Bottom Margin</code>、<code>Top Margin</code>、<code>Trailing Margin</code> 和 <code>Leading Margin</code>,当我运行我的应用程序时,我觉得这很奇怪图片顶部和底部的空间。</p>

<hr/>

<p>这里是一些截图:</p>

<p> <img src="/image/42c25.png" alt="Full Main.storyboard screenshot"/> </p>

<p> <img src="/image/YWVnr.jpg" alt="iOS simulator screenshot"/> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我建议您为此尝试使用 UICollectionView,因为对于此类布局,最好使用 Collection View 而不是 TableView ,试试这个(tempArr 表示您的图像数组),</p>

<pre><code>func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -&gt; CGSize {
    // create a cell size from the image size, and return the size

    let picHeight = tempArr.valueForKey(&#34;imgHeight&#34;) as! String
    let picWidth = tempArr.valueForKey(&#34;imgWidth&#34;) as! String
    var picHeightFloat :CGFloat!
    var picWidthFloat :CGFloat!

    if let n = NSNumberFormatter().numberFromString(picHeight) {
      picHeightFloat = CGFloat(n)
    }
    if let q = NSNumberFormatter().numberFromString(picWidth) {
      picWidthFloat = CGFloat(q)
    }

    let imageSize = CGSize(width: picWidthFloat, height: picHeightFloat)


    return imageSize
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何让 UIImageView 填充 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43025403/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43025403/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何让 UIImageView 填充 UITableViewCell?