我正在尝试设置一个可搜索的 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/