如何调整imageView的大小?

问题描述 投票:-1回答:3

如何调整imageView的大小? Xcode表示从未使用过imageView的初始化。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    let city = nicosiaPlaces [indexPath.row]
    cell?.textLabel?.text = city
    let imageView = CGSize(width: 100, height: 100)
    cell?.imageView?.image = UIImage(named: city)
    return cell!
}
swift imageview
3个回答
0
投票

您只需在每次调用方法时创建一个新的CGSize,您应该改变单元类中的大小。如果您将故事板与AutoLayout一起使用,则应更改故事板中的大小。


0
投票

我猜你的问题里面有一些缺陷。

假设您需要调整Imageview的大小,我会为您发布小小片段。

永远不要在每次创建新实例时初始化func tableView(cellForRowAt:IndexPath)中的任何新组件,直到它们被妥善处理。您已作为let imageView = CGSize(--,--)参考,但您再次从Cell Method调用该实例。

不要那样做。

  override func awakeFromNib(){
      let imageView = UIImageView(CGRectMake(0,0,150,150))
      imageView.contentMode = .scaleAspectFill
      contentView.addSubView(imageView)
}

现在在Class方法中获取ImageView的引用并根据需要调整大小。

      overrride  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
      {
         let cell = tableView.deququeResuableCell(withIdentifier: "cellID",forIndexPath: indexPath) as! yourCustomCellClassName
          cell.imageView?.image = UIImage(named: "yourImage")
       // If you need to resize the Image make changes in Frame of Image or ContentMode of the Image.
         return cell
        }

-1
投票

您的代码目前只是创建一个CGSize,下面的第一行(从您的问题中复制),而第二行是指您单元格内的imageView。

let imageView = CGSize(width: 100, height: 100)
cell?.imageView?.image = UIImage(named: city)

您的目标是调整已经在单元格内的imageView,还是创建一个新的?

© www.soinside.com 2019 - 2024. All rights reserved.