如何调整imageView的大小?

时间:2018-07-07 19:01:36

标签: swift imageview

如何调整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!
}

3 个答案:

答案 0 :(得分:0)

每次调用该方法时,您只需创建一个新的CGSize,而应该更改单元格类中的大小。而且,如果将情节提要与AutoLayout一起使用,则应更改情节提要中的大小。

答案 1 :(得分: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
        }

答案 2 :(得分:-1)

您的代码当前正在创建CGSize,第一行位于下面(从问题中复制),而第二行引用的是单元格内的imageView。

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

您的目标是要调整单元格内部的imageView的大小,还是创建一个新的imageView?