ios - 表格 View 单元格中的 Storyboard ImageView

标签 ios swift uitableview uiimageview storyboard

我在 TableView 的 TableView 单元格中有一个 ImageView 。我想要实现的是它应该根据它动态包含的图像调整 ImageView 的大小。目前我有这样的:

enter image description here

最佳答案

首先将底部内容设置为这样的高度,

enter image description here

然后像这样设置图像约束,

enter image description here

然后在您的代码中执行此操作,

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        let imageNew  = UIImage(named: "test") //Set your image here
        let oldWidth = imageNew!.size.width
        let scaleFactor = tableView.frame.size.width / oldWidth

        let newHeight = imageNew!.size.height * scaleFactor
        let newWidth = oldWidth * scaleFactor

        //Finally to get cell size just add the bottom part height for othercontents to ImageHeight here
        let CellSize = CGSize(width: newWidth, height: (newHeight + 40))
        return CellSize.height

    }


     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
        cell.NewImage.image = UIImage(named: "test")
        return cell

    }

你应该得到这样的东西, enter image description here

关于ios - 表格 View 单元格中的 Storyboard ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41179990/

相关文章:

swift - 如何快速将单词分解为字符

ios - 在不知道高度的情况下在单元格内异步下载图像

iphone - 如何在编辑模式下向 UITableViewCell 添加 subview 并调整其大小?

ios - 如何在iOS项目中正确实现Stripe?

c++ - 如何为 iphone 编译一个简约的 cpp Protocol Buffer 库?

iPhone - 根据传递给它的数据动态调整 UILabel 内容

swift - 如何使用 Swift 的类似 'abstract class' 的协议(protocol)扩展来访问方法中的静态变量

swift - 使用prepareForSegue将特定数据传递到另一个Viewcontrol

ios - 使用图像和标签设置 Tableview 背景

ios - 如何在 Qt Creator 中指定 iOS 模拟器?