简体   繁体   中英

UITableView Cells not Autoresizing With Label Text

EDIT EDIT: I started from scratch in a new project. Every storyboard outlet is identical, as is the VC code. It works, interestingly.

EDIT: hello everyone, I am giving up. Every answer is starting to become a variation on a theme, and that theme is not fixing this. Thanks for the attempt though.

In ViewDidLoad , I have set: tableView.estimatedRowHeight = 85.0 , tableView.rowHeight = UITableViewAutomaticDimension

I have also, in storyboard, set: the auto-resizing constraints to the content view. 在此处输入图片说明

the label to wrap, and the label lines to 0

Yet still, the label text is overflowing its cell. The complete string is: "HELLO? WHAT HAPPENS IF I WRITE A LONG SENTENCE?"

在此处输入图片说明

Any tips?

Here is the entire view controller: 在此处输入图片说明 And here is the storyboard with the label selected: 在此处输入图片说明

after the recommended functions: enter image description here the result: enter image description here

You are not setting the heightForRowAt indexPath.. for that you should first get the size of your text in my case i got the size through

func rectForText(text: String, font: UIFont, maxSize: CGSize) -> CGSize
{
    let attrString = NSAttributedString.init(string: text, attributes: [NSAttributedStringKey.font:font])
    let rect = attrString.boundingRect(with: maxSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil)
    let size = CGSize(width: rect.size.width, height: rect.size.height)//(, )
    return size
}

and set the height of cell according to your text, in my case

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    var totalHeight : CGFloat = 0
    let str = yourString
    let size : CGSize = rectForText(text: str, font:UIFont.boldSystemFont(ofSize: 15.0) , maxSize : CGSize(width: 200 * (SCREEN_WIDTH / 320), height: 20000) )
    totalHeight = size.height
    return CGFloat(totalHeight)
}

might be work for you

Try adding constraints to the label that's in the content view.

Eg: left - 8 pts top - 8 pts right - 8 pts bottom - 8 pts

Now it should stay within the cell.

You need to add heightForRowAtIndexPath to automatically adjust cell height to its content.

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

Look at the screenshot of my output. For the label, you should give all the four constraints(top:5, bottom:5, left:5, right:5) and you have to change the heights of the label's priority to 750 & set the hugging and resistance priority to 1000. Then you have to change the number of lines to 0 in storyboard

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return 250

    }

在此处输入图片说明

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