如何让UIImageView填充UITableViewCell?

时间:2017-03-26 05:35:06

标签: ios swift uitableview uiimageview

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

我正在制作一个照片应用,我想让照片填满UITaleViewCells,然后展开细胞以适应图像。

我一直在努力让它发挥作用,我已经搜索了所有我能做的事情,观看了一些YouTube视频,但我找不到任何与UIImageView相关的工作。这是我正在使用的代码:

override func viewWillAppear(_ animated: Bool) {
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension
    }

在UIImageView上我有这些约束: Bottom MarginTop MarginTrailing MarginLeading Margin,当我运行我的应用时,我会在图像的顶部和底部看到这个非常奇怪的空间。

以下是一些截图:

Full Main.storyboard screenshot

iOS simulator screenshot

1 个答案:

答案 0 :(得分:0)

我建议您尝试使用UICollectionView,因为对于这样的布局,最好使用Collection View而不是Table Views,试试这个(tempArr表示你的图像数组),

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

    let picHeight = tempArr[indexPath.row].valueForKey("imgHeight") as! String
    let picWidth = tempArr[indexPath.row].valueForKey("imgWidth") 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
}
相关问题