简体   繁体   中英

iOS8 Dynamic Cell Height based on Dynamic Label height

I'm trying to build a feed that follows this:

在此处输入图片说明

The top left label is a user name, top right is a timestamp. Right below is a descriptor, and then the 3rd label down is the dynamic post body. The two buttons on the bottom each do something that alters the label right above.

Two things I'm trying to accomplish: I've successfully used auto-layout to constrain the top left label to the top and left. I've successfully used auto-layout to constrain the top right label to the top and right. I have an explicit width set on the top left layout so that if the user name is too long, it will truncate before the top right label. First question: Is it possible to make the width dynamic such that the top right label stays plastered to the top and right part of the cell, and the top left label truncates if it is within a centimeter from the top right label? This is important for different screen sizes.

Next: I have successfully established the dynamic height label (3rd one down from the top) to have a width of the entire cell by constraining it to the left and right margins. I tried constraining it on the bottom also, but this is where I run into my huge issue. The text leaks into the next cell if it is too long. I know why this happens, but I don't know how to fix it.

If I don't have:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 128
}

in my code, then I run into this: "Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead."

However, when I place that code into my View Controller, the label does have a dynamic height, but because there is a constant height to each row, it leaks through. I don't know how to mitigate this because without that line, the warning as stated uses the standard height instead, which lumps all my labels and buttons together all on top of themselves (and then for some reason, the dynamic label text no longer has a dynamic height, but instead conforms to one line).

How do I fix this issue? I've been puzzled and stuck on this for a long while.

  • First question: Yes, it is. You'll need to use different horizontal compression resistance priorities. You might also need a lower priority inequality to set a limit.

  • Second question: The problem is that you are not fully determining the height. You must have constraints all the way from the top of the cell to the bottom of the cell, so that the cell's height is rigidly determined by its contents from the inside out.

For setting dynamic height for cell, just set the dynamic height and width for it's label. By setting

  1. the content hugging priority of label is less than 1000.
  2. And also avoid to set constraints for height and width to label.
  3. Just set constraints for Top,left and if the bottom element is availabel then also set the bottom constraiants.

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