如何以编程方式添加 imageView 宽度/高度?

Kyr*_*dis 0 uitableview uiimageview image-size swift

我如何更改width/heightUIImageView. 我有很多不同的图片width/height,我希望它们以 imageView 的大小显示。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    let city = nicosiaPlaces [indexPath.row]
    cell?.textLabel?.text = city
    cell?.imageView?.image = UIImage(named: city)
    cell?.imageView?.frame.size = CGSize(width: 10, height: 10)
    return cell!
}
Run Code Online (Sandbox Code Playgroud)

我想让这行代码工作:

cell?.imageView?.frame.size = CGSize(width: 10, height: 10)
Run Code Online (Sandbox Code Playgroud)

Car*_*van 5

代替:

cell?.imageView?.frame.size = CGSize(width: 10, height: 10)
Run Code Online (Sandbox Code Playgroud)

和:

cell?.imageView?.translatesAutoresizingMaskIntoConstraints = false
cell?.imageView?.heightAnchor.constraint(equalToConstant: 10).isActive = true
cell?.imageView?.widthAnchor.constraint(equalToConstant: 10).isActive = true
Run Code Online (Sandbox Code Playgroud)

看起来您希望cell是可选的,但如果上述方法不起作用,请尝试删除问号。