简体   繁体   中英

How can I achieve dynamic cell height and image height with UIView in UITableView?

I have some difficulty in figuring out how to dynamically have each cell resize according to the UIImage and the other content inside the UIView .

Heres a picture to better understand:

The grey background is the contentView of the cell .

The white on top is a UIView .

在此处输入图片说明

Here is how I am downloading images:-

inside cellForRowAtIndexPath

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

        }];

Setting the height of the cell in heightForRow according to the size of the cell from storyboard

return 535.0f;

Provide the estimatedRowHeight and rowHeight of your tableView as

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

while setting up your tableView .

Update:

For top left image: Leading to UIView, Top, Trailing to button, Width and height For Button: Width, height, top, trailing to Label

For Label: Width, height, top, trailing to UIView

For Bottom Button: leading to UIView, trailing to Label, Bottom to contentView, width, height

For Bottom Label: Trailing to UIView, Bottom to Content View, Width , Height

For ImageView: Leading to UIView, Trailing to UIView, Top to any of items above image View, bottom to "Bottom Button" or "Bottom Label". Set its width equal to content View. No Height, Set its content Hugging property to low.

// try like thhis

- (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;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM