Group Group Group Group Group Group Group Group Group
raywenderlich.com Forums

Self-sizing Table View Cells

In this tutorial you'll learn how to enable self-sizing table view cells, as well as how to make them resize on-demand, and support Dynamic Type.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1067-self-sizing-table-view-cells

This was a useful tutorial for me on auto layout and Swift (coming from 16 years of Objective-C and a two year developer hiatus). Thanks!

I had to add a couple of lines to ArtistDetailViewController’s cellForRowAtIndexPath: to initialize the appearance behavior of the moreInfo textView.

cell.moreInfoTextView.text = work.isExpanded ? work.info : moreInfoText
cell.moreInfoTextView.textAlignment = work.isExpanded ? .Left : .Center

Maybe this is a little out of blue. But,can you tell me in ‘Evernote’ App Detail View
First there is UITextView and then there are as many images as you want to add.So, is it a scrollview or some kind of table view, because no. of images a user will add are not certain.

Cool stuff, thanks! Would be even more awesome if you could do one for UICollectionView cells too…

Hey Bradley… This tutorial was fantastic… Exactly what I’ve been looking for!

Thanks
~Martin

Hi,

Nice tutorial.
I have one question, I have 3 different custom cells to be shown in different sections, the cells in each sections are different and have different heights. In this case how we can handle?
what value should i put
tableView.estimatedRowHeight = ?? which tablecell’s height?

Hello, thank you for this tutorial. Is it possible to see somewhere the construction of the table view before resizing the cells. I am especially interested on the construction of the organic label with multigne text just inside. I feel some worries multignes have a label in a tableview and I saw that you use for bio Json.
Thank you in advance

Hi Bradley,

Thanks for the tutorial. I am new to swift. I used your instructions to create a very simple tableview with three images of different sizes. I want to dynamically change the height of the cell such that the image fills the imageView in the width and image maintains the aspect ratio. Based on your instructions this seems to be a straight forward task. But in my case, height of the cell doesn’t adjust to perform this task. Either the image fills with aspect ratio leaving a lot of width space empty or fills the width but clips the part of the image vertically. I made sure there are 4 leading, trailing, top and bottom constraints and also the tableView.rowHeight and table.estimatedRowHeight is set in viewDidLoad. Would it be possible for you take a look at the project that I have uploaded at this link and suggest what could I be missing? https://github.com/Neeraj3pathi/SimpleDrawing/issues/2

Thanks.

Why oh WHY SWIFT 2.0 when Swift 3.0 is coming soon and Swift 4.0 will come next year and DE-PRE-CATE all Swiftly.

Please stick with some production ready code, not “swift”. It’s not even a proper stable programming language yet.

But thanks a LOT or the info! It’s really helpfull, just have to google it back to “objective” C

Very Helpfule! Thanks!

it is greate! thanks.

Hi, do you have the link to the original tutorial? (Joshua Greene)

Thank you so much Bradley!
Very well explained!

Hi. This guide is very helpful to me.
While I follow your guide, I got a very strange behavior.
At the middle of guide, which said

Build and run the app. This screen is now looking much better, but scroll down to Georgia O’Keeffe and you will notice something weird:

The guide said that it should look like the screenshot attached in the guide, which the nameLabel height is stretched.

But when I kill the running app and run again, with THE SAME APP JUST BUILT BEFORE, sometimes it looks like this.

I tested to to figure out when this behavior happened but ends in fail. It happens totally randomly.

Anybody knows why this happens?

I have a new questions.

At the last of “Show me the Art!” section, the guide instruct me to write this code

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300

Then, the table cell expanded. But not exactly what I defined.
Run simulator, touch the artist and see their works. We defined the workImageView’s Top and Bottom space equal to 8. Right? But the actual space is not 8. Please see the vertical red line of this screenshot

The margin of nameLabel and moreInfoTextView are fine. But only the workImageView’s margin is not 8. I tried to various composition of top&bottom space priority, content hugging priority, but end in fail.

Can anybody solve this problem?

I have a question:
when clicking the moreInfoText in simulator iPhone 7 Plus -iOS 10.0, the Artist detail tableView will shake,other simulator not shake,why?Is it cause of “tableView.beginUpdates() tableView.endUpdates()”?

Hi,

I downloaded your project, made a few changes, and run it on ios 8.1 device, there is some inconsistency that im not able to solve.


i updated your artist list screen to display 2 labels in a line, and kept rest of the functionality same, seems like there is some issue.

the cells does not resize properly

I have attached screenshots for ios 8 and 10.

but when i scroll up, and come down again, it gets proper.

How can I use self-sizing to hide a row (set height to 0)?

Hi, this works great with those images, But If im using larger images likes this:
Download link of image.

It doesn’t work as expected, how can we solve this?

With the image I attach on the link The App looks like:

Because Image is larger.

Any clue on how to make this work for any size image?