swift - 如何在 UITableView Cell 中为 ImageView 添加大小和位置?
问题描述
我在我的 UITableView 单元格中添加了一张图片:
cell.imageView!.image = UIImage(named: "Default.png")
一切正常,图像显示。现在我想为图像提供一个自定义大小和位置,所以我尝试使用以下代码:
cell.imageView!.frame = CGRect(x: 29, y: 17, width: 14, height: 13)
但无论出于何种原因,它都不起作用。有什么建议吗?
解决方案
如果你的位置不会改变你可以在你的TableViewCell
class TableViewCell: UITableViewCell {
@IBOutlet weak var imageView: UIImageView!
override func layoutSubviews() {
super.layoutSubviews()
self.imageView?.frame = CGRect(x: 12, y: 23, width: 12, height: 100)
}
}
您可以更改框架,tableview(cellForRowAt)
但如果框架不会改变它的坏处,因为cellForRowAt
在向上滚动或加载单元格时运行。您可以在 UITableViewCell 中定义一次。
推荐阅读
- r - R one-liner 用于反转列表的嵌套顺序?
- google-sheets - 如何根据 Google 表格中相邻列中的值返回一系列单元格
- java - 按下按钮时没有任何反应
- java - 使用 Jackson 库时如何实现错误处理程序
- reactjs - 如何使用 Jest 使用新的 React Router Hooks 模拟 history.push
- r - 如果列标题位于两个日期之间,则返回值
- c# - 如何使用 System.Net.Http.HttpRequestMessage 检索 Cookie
- mongodb - Mongodb 从数组中的数组投影对象 - 嵌套数组
- c# - 查找列表中匹配表达式的连续元素
- excel - 使用 SUMIFS 聚合数据