简体   繁体   中英

How to set UITextView's height dynamically in UITableViewCell based on string size?

I understand that this question has been asked here , but it didn't solve my problem as the link in the accepted answer is down and the minimal example didn't help.

Here is a picture of my custom UITableViewCell & all its constraints:

在此输入图像描述

Each post contains these UI elements. The only element that could make each cell's height different is messageView , because its height depends on the string being displayed. Question is, how do I dynamically set each cell's height? Here's what I have now (Does NOT work, messageView is not shown at all):

func cellForRowAt(indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(...) as! PostCell

    let message = ...
    cell.messageView.text = message

    return cell
}

func heightForRowAt(indexPath: IndexPath) -> CGFloat {
    var cellMinimumHeight: CGFloat = 120
    if let cell = tableView.cellForRow(at: indexPath) as? PostCell {
        let size = cell.messageView.sizeThatFits(cell.messageView.frame.size)
        cellMinimumHeight += size.height
    }

    return cellMinimumHeight
}

in heightForRowAt function, the if is not being executed, therefore, all cells' heights are cellMinimumHeight = 120 .

How could I make each cell's height = 120 + messageView's height?

---------------------------------EDIT 1---------------------------------

Made a mistake in the picture, messageView 's height is not set

When using Auto-Layout for dynamically sizing cells, you don't really need to implement sizeThatFits(...) . If the constraints are setup correctly, then you only need to disable the scrolling of the UITextView .

From code:

yourTextView.scrollEnabled = false

From IB:

Select your Text View and open Attributes inspector , then

在此输入图像描述

In Attributes Inspector select your UITextView(messageView) and Uncheck "Scrolling Enabled".

And then change your UITextView(messageView)'s content compression resistence priority as follows: Horizontal = 750 Vertical = 1000

I hope this will help you.

Just disable UITextview scroll... But here is no use of UITextview, you can use label also.

In HeightForRow tableview delegate method remove that stuff and use

return UITableViewAutomaticDimension

You have used constrain to make cell hight dynamic

form apple documentation

To define the cell's height, you need an unbroken chain of constraints and views (with defined heights) to fill the area between the content view's top edge and its bottom edge.

So for your case you need

cell's height = 120 + messageView's height?

So start from Profile Image to measure unbroken chain of constraints from Top to Bottom

  1. Profile Image top = 10 + ImageHeight = 60 ----> 70

  2. MessageView top = 10 + set minimum height to message say 20 one line if every cell should have message even if one word and set this height Greater than or equal to 20 make sure that you set Scroll enable = false so message Height minimum = 10 top + 20 + 10 bottom ---> 40

  3. Menu Stack view Height ---> 30

So all Total = 70 + 40 + 30 = 140 this default hight no cell will be less than this

Also you must set the table view's rowHeight property to UITableViewAutomaticDimension . You must also assign a value to the estimatedRowHeight property

tableView.estimatedRowHeight = 130.0
tableView.rowHeight = UITableViewAutomaticDimension

Here is apple documentation Here

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