如何根据下载的图像大小创建动态大小的行高? [英] How to create dynamically sized row heights based on downloaded image sizes?

查看:20
本文介绍了如何根据下载的图像大小创建动态大小的行高?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据从 firebase 下载的图像的纵横比在我的 UITableView 中创建动态大小的行高.我下载 func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) 设置单元格行高的正确位置在哪里?

I’m trying to create dynamically sized row heights in my UITableView based on the aspect ratio of downloaded images from firebase. I download the images inside of the func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) where is the right place to set the rowheights of the cells?

推荐答案

  1. 下载图像后,重新加载相应的行
  2. tableView:heightForRowAtIndexPath: 中根据下载图像的大小返回一个值
  1. Once you've dowloaded an image reload the corresponding row
  2. in tableView:heightForRowAtIndexPath: return a value based on the size of the downloaded image

更新

为您的表维护一组模型对象.每个模型对象都应包含对图像的引用.在 cellForRow 中检查对应的模型对象.如果它包含图像,则将其分配给图像视图.否则开始下载图像.下载完成后,将图像放入模型对象并重新加载该行.

Maintain an array of model objects for your table. Each model object should contain a reference to an image. In cellForRow check the corresponding model object. If it contains an image then assign it to the image view. Otherwise start downloading the image. Once it is downloaded put the image into the model object and reload the row.

heightForRowAtIndexPath: 中也检查模型对象.如果它不包含图像,则返回一些默认高度.否则返回图像的高度.

In heightForRowAtIndexPath: check the model object as well. If it does not contain an image then return some default height. Otherwise return the height of the image.

至于 rowHeight 属性,它将为表格的每一行设置高度,这绝对不是您需要的

As for rowHeight property it will set height for each row of the table which is defenitely not what you need

这篇关于如何根据下载的图像大小创建动态大小的行高?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆