OGeek|极客世界-中国程序员成长平台

标题: ios - 如何让 UIImageView 填充 UITableViewCell? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:20
标题: ios - 如何让 UIImageView 填充 UITableViewCell?

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

我正在制作一个照片应用程序,我希望照片填充 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



Best Answer-推荐答案


我建议您为此尝试使用 UICollectionView,因为对于此类布局,最好使用 Collection View 而不是 TableView ,试试这个(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
}

关于ios - 如何让 UIImageView 填充 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43025403/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4