Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xcode - Swift, Auto Resize Custom Table View Cells

In my app, I have a table view with an image, label and text view in each cell. I would like to be able to auto-resize the cells depending on the amount of content in the text view. (The text view is the lower most text.)

So far, I have added the correct constraints; leading, trailing, top and bottom to the text view and have disabled scrolling and editing.

In my tableViewController.swift file, I have written this code:

override func viewWillAppear(_ animated: Bool) { tableView.estimatedRowHeight = 100 tableView.rowHeight = UITableViewAutomaticDimension }

However, this is not working as when I add more text to the text view, it just cuts off.

Maybe this has something to do with the card like look, I have got a UIView in each cell acting as a card.

I honestly don't know what I am doing wrong

A picture is below of what my app looks like and if anyone could help that would be greatly appreciated

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check if your constraints are like this(based on your image) :

imageView : set to Top and Leading of your container, with fix height and width values.

label : you can set it to top and horizontal space of your image, with fix height and width as well.

textView : leading to image, top space to label, trailing and bottom to container.

And keep using

tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableViewAutomaticDimension

in your viewWillAppear()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...