我正在尝试建立一个可搜索的TableView
。我希望显示屏显示公司名称,日期和徽标。我的代码工作正常;但是,我的所有徽标都有不同的尺寸,因此,我的界面看起来非常草率。我可以为表格视图中的每个图像设置统一的大小吗?与Instagram的搜索页面类似。
我试过用:
cell.imageView?.image = UIImage(data:self.filteredCakes[indexPath.row].imageName, scale: CGRectMake(0, 0, 40, 40))
但是,我遇到了错误:
“无法将'String'类型的值转换为预期的参数类型'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
}
我希望图像尺寸统一,同时仍能正确显示公司徽标。谢谢!
在图像视图上设置高度和宽度约束。然后将其contentMode
设置为UIView.ContentMode.scaleAspectFit
。