ios - 是否可以在 swift/Xcode tableview 中指定一个恒定的图像大小?如果是这样,我该怎么做?

标签 ios swift uitableview image-resizing

我正在尝试设置一个可搜索的 TableView。我希望显示屏显示公司名称、日期和他们的 Logo 。我的代码中有这个工作;然而,我所有的标志都是不同的大小,因此,我的界面看起来很草率。我可以为表格 View 中的每个图像设置统一大小吗?类似于 Instagram 的搜索页面。

我尝试使用:

cell.imageView?.image = UIImage(data:self.filteredCakes[indexPath.row].imageName, scale: CGRectMake(0, 0, 40, 40))

但是,我遇到了错误:

"Cannot convert value of type 'String' to expected argument type 'Data'"

变量 imageName 是一个与我的 Assets 文件夹中的图像关联的字符串

这是我的代码,可以运行,但会显示不同的图像尺寸:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell     {
    let cell: UITableViewCell = UITableViewCell(style:                         UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")

        cell.textLabel?.text = self.filteredCakes[indexPath.row].businessName
        cell.detailTextLabel?.text = self.filteredCakes[indexPath.row].dateText
        cell.imageView?.image = UIImage(named:self.filteredCakes[indexPath.row].imageName)

        return cell
}

我希望图片尺寸统一,同时仍能正确显示公司 Logo 。谢谢!

最佳答案

在 ImageView 上设置高度和宽度约束。然后将其 contentMode 设置为 UIView.ContentMode.scaleAspectFit

关于ios - 是否可以在 swift/Xcode tableview 中指定一个恒定的图像大小?如果是这样,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55368633/

相关文章:

iphone - 以编程方式更改 UITableView 的滚动插图以反射(reflect) iPhone 键盘的大小

ios - SWIFT:iPhone 被握住或放在 table 上 “upwards”

ios - 如何在ipad上以单应用模式打开设置

Swift 将整数转换为 2 个字符的十六进制字符串

ios - 如何更改 UICollectionView 中单元格之间的空间?

ios - 在相同类型的 3 个独立静态 TableView 单元格上设置内容

ios - 如何在TableController的ImageView中正确添加图片?

ios - 仅在 UISplitViewController 的 DetailViewController 中呈现一个 View Controller

ios - 如何将自签名根证书颁发机构添加到设备?

ios - Charles Map Local 不适用于 JSON 数据?