【问题标题】:cell.imageView!.frame size change. Swiftcell.imageView!.frame 大小变化。迅速
【发布时间】:2016-02-25 18:54:01
【问题描述】:

单击单元格后,我找不到 cell.imageView 更改的任何原因。似乎只有宽度变化。在我的代码中,图像帧应该是第二张图片中的 iPhone。

imageView 是 Swift 内置的,不是我在 tableviewcell 中的自定义名称。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    if tableView == resultsTable {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MarketCell")
        var supplyDetail = String()

        var frame = cell.imageView!.frame
        let imageSize = 50 as CGFloat
        frame.size.height = imageSize
        frame.size.width  = imageSize
        cell.imageView!.frame = frame
        cell.imageView!.layer.cornerRadius = imageSize / 2.0
        cell.imageView!.clipsToBounds = true

        cell.imageView?.image = UIImage(named: "Profile Picture")
        ...getDataInBackgroundWithBlock...
            cell.imageView?.image = UIImage(data: data!)!
        return cell

    } 
}

点击前,

点击后,

【问题讨论】:

  • 您发布的代码建议图像视图应始终为 50 x 50,但您说第二张图像是正确的,但只有一个图像视图是 50 x 50。发布与设置相关的所有代码图像视图的大小。
  • 正确的应该是第二张图片中的iPhone。这就是与图像大小相关的所有代码。我试图划掉从 var frame 到 cell.imageView.clipToBounds 的所有行。同样的事情也会发生。我想知道属性检查器有什么需要注意的吗?
  • 请不要显示虚假的代码摘要。显示真实代码。
  • 已更新。它很长。这就是我总结它的原因。哈哈
  • 啊。你在使用自动布局吗?

标签: swift image uitableview uiimageview cell


【解决方案1】:

当您使用 UITableViewCell 的 imageView 属性时,您在某种程度上接受了 Apple 的条款。也就是说,它将被调整为单元格的高度,并出现在 contentMode = .ScaleAspectFit 的任何默认标签的左侧。要将 UIImageView 的大小调整到您自己的规格,您可以继承 UITableView 并创建自己的原型单元格,我个人的建议,或者您可以像这样简单地覆盖 layoutSubviews:

override func layoutSubviews(){
    super.layoutSubview()
    self.imageView.frame = CGRect(x:0.0,y:0.0,width:40.0,height:40.0) //Or whatever frame you think is necessary
}

您也可以将 UIImageView 作为子视图添加到 cell.contentView 并在 cell.contentView 出列后删除所有子视图。

【讨论】:

  • 我明白你的意思。现在,我想根据单元格的高度保持图片大小。所以,我删除了改变 imageView 大小的代码,但同样的事情发生了。这正常吗?
  • 嗨。我找到了导致这种情况发生的原因。它来自 getDataInBackgroundWithBlock。我换了试试! ...获取数据()。然后它就完美了。
猜你喜欢
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多