【问题标题】:How can I achieve dynamic cell height and image height with UIView in UITableView?如何在 UITableView 中使用 UIView 实现动态单元格高度和图像高度?
【发布时间】:2016-05-31 09:18:32
【问题描述】:

我很难弄清楚如何根据UIImageUIView 中的其他内容动态调整每个cell 的大小。

这是一张更好理解的图片:

灰色背景是cellcontentView

顶部的白色是UIView

这是我下载图像的方式:-

cellForRowAtIndexPath

cell.bigImageView.file = (PFFile *)object[@"image"];
[cell.bigImageView loadInBackground:^(UIImage * _Nullable image, NSError * _Nullable error) {

        }];

根据storyboard的单元格大小设置heightForRow中单元格的高度

return 535.0f;

【问题讨论】:

    标签: ios objective-c uitableview uiview


    【解决方案1】:

    将您的tableView 的estimatedRowHeightrowHeight 提供为

    tableView.estimatedRowHeight = 100.0 // Your estimated height of the row
    tableView.rowHeight = UITableViewAutomaticDimension;
    

    在设置您的 tableView 时。

    更新:

    对于左上角的图片:UIView、Top、Trailing to button、Width 和 height 对于按钮:宽度、高度、顶部、尾随到标签

    对于标签:宽度、高度、顶部、尾随到 UIView

    对于底部按钮:通向UIView,尾随Label,底部至contentView,宽度,高度

    对于底部标签:尾随到 UIView、底部到内容视图、宽度、高度

    对于 ImageView:通向 UIView,尾随到 UIView,顶部到图像视图上方的任何项目,底部到“底部按钮”或“底部标签”。设置它的宽度等于内容视图。没有高度,将其内容拥抱属性设置为低。

    【讨论】:

    • 这也会调整 uiimage 和 uiview 的大小?
    • 是的,但是不要忘记将 UIView 的尾随、前导、顶部、底部约束与其 ContentView 相关。
    • 我是否也对 ContentView 的图像、标签、按钮进行约束?
    • 不,UIView 中所有内容的约束都应与该 UIView 相关
    • 我是否让 uiimageviews 拥抱水平或垂直低
    【解决方案2】:

    // 像这样试试

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      UIImage *img = // here load image
      if (img) {
          return img.size.height + // your additional static heght
      } else {
       return 350;// defaultHeght;
      }
    }
    

    【讨论】:

    • 如果我有多个图像,此答案将不起作用。
    • 加载每个图像后,您需要重新加载相应的单元格
    • 要加载图像,您需要编写一个方法,从 heightforrowAtindex 方法中调用该方法,在加载成功时您需要重新加载该单元格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多