ios - 具有不同 UITableViewCells 和动态内容的 UITableView
问题描述
在我们的应用程序中,我们经常使用具有类型的显示项目列表作为数据源,在可滚动列表中UITableViews
显示不同的自定义。UITableViewCells
然后在TableView
'scellforRowAt
函数中,我们UITableViewCell
根据显示项目的类型决定使用哪个自定义。
这种方法现在会导致UITableViewCell
从 Internet 加载和显示图像的问题,因为视图还不知道图像的大小。
这是我正在尝试构建的自定义视图:请参阅自定义 UITableViewCell
但是,我尝试仅从自定义视图中的图像开始。由于该TableView
行不知道图像将有多大,因此视图出现时不会显示图像。只有在向下和向上滚动到图像应该在的位置之后,单元格才会按照我想要的方式调整大小。我将图像的顶部、前导和尾部约束设置为 0,以便图像完全填充单元格。
加载图像并在此类单元格中显示它们的最佳做法是什么?根据实际图像大小调整单元格的大小?
在viewDidLoad
我设置
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80
cellForRowAtIndexPath
let cell: CustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
cell.imageView.contentMode = .scaleAspectFit
tableView.beginUpdates()
cell.imageView.kf.setImage(with: URL(string: item.getImageUrl()), placeholder: nil, options: nil, progressBlock: nil) { (image, error, cacheType, url) in
tableView.endUpdates()
}
cell.isUserInteractionEnalbed = false
cell.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: .greatestFiniteMagnitude)
cell.accessoryType = .none
return cell
解决方案
推荐阅读
- azure - 使用 ansible playbook 更新 azure windows vm 中的磁盘存储类型
- php - HTML/PHP 输入文件不传递现场拍摄的照片(手机)
- reactjs - 当应用程序在运行时使用组件加载器时如何应用 tree-shaking (webpack)?
- django - 自定义分页在 Django rest 的 apiview 中不工作
- json - 用于提取 JSON 数组值的 AWS Glue 分类器
- r - 如果一列上的前一个值大于另一列中的下一个值,则使其等于对应列中的值
- mysql - 多个表之间 CDC 事件的 Debezium 排序
- ansible - 如何从字典列表中获取子集字典?
- python-3.x - pyqtgraph 交叉小部件绘图不起作用?
- python - 张量流在损失函数中使用输入