How to resize the imageView?

别说谁变了你拦得住时间么 提交于 2019-12-12 07:02:57

问题


How do I resize the imageView? Xcode says initialisation of imageView was never used.

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!
}

回答1:


You just create a new CGSize every time when the method is called, you should change the size in your cell class instead. And if you're using storyboard with AutoLayout, you should change the size in storyboard.




回答2:


I guess your question has some flaws inside.

Assuming that you need to resize an Imageview I'm posting small Snippet for you.

Never initialize any new component inside func tableView(cellForRowAt:IndexPath) beacuse every time it is going to create a new Instance until they are properly disposed. You have taken reference as let imageView = CGSize(--,--) but you are again calling the instance from Cell Method.

Don't do that.

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

Now In the Class method get reference of the ImageView and resize if you want.

      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
        }


来源:https://stackoverflow.com/questions/51226153/how-to-resize-the-imageview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!