When I want to have a custom cell, I generally add a UIView subclass to the cell's content view. For the layout of my subviews I use a nib. Then I wire up the nib to my UIView subclass. My issue is how to dynamically size content. Say my view has a lot UILabels inside it. I use layoutSubviews to position all the subviews - but it is only until that is done that I truly know the height of my cell. So currently in tableView:heightForRowAtIndexPath I setup my subview and call layoutIfNeeded so everything is positioned properly. Now I know the height of my cell and return it in the tableView:heightForRowAtIndexPath method. But now when tableView:cellForRowAtIndexPath is called the cell that I am given has a height of 44.0. When I added my subview to it - my subview is outside of its parent's bounds. Then when the cell is later resized in iOS to the height that I said I needed, my content is thrown off because of my autoresizingMask. Just trying to figure out if this is an issue others deal with or if I'm approaching it completely wrong. It just seems backwards that we ask for the height, then create a cell that is not that height.
Unfortunately, this is how UITableViews work: You need to provide the heights before the UITableViewCells are actually rendered.
And yes, everyone has to deal with it. :)
You could create an NSArray, add all your custom contentViews, set their frame according to the expected contentView bounds and then use this array as data source in tableView:heightForRowAtIndexPath: and tableView:cellForRowAtIndexPath:. While this isn't exactly efficient, it works fine for small data sets.
Also, here's a nice tutorial with this topic:
UITableViewCell Dynamic Height (by Matt Long)
A similar question on SO:
How can I do variable height table cells on the iPhone properly?
Related
I've got a table cell. Within it I've got an image that has a height constraint of 25% of the screen size.
When is the proper time to retrieve the image height using
imageView.frame.height
to make sure that it has been resized according to the constraint? In general i am a little confused as to when the time to retrieve the element heights (images, labels ) is in the lifecycle. I need this information to add up with all the other element heights for a cell so i can tell a table what the row size is using the heightForRow method.
P.S: I cannot use UITableViewAutomaticDimension as one of the elements is a webview and i can only retrieve its height after the webview delegate method completes and has loaded a webpage.
If you are using autolayotus, you can get imageView or any other UIElements frames in viewDidLayoutSubViews or in viewDidAppear. These life cycle methods will be called while/after applying constraints.
You can always subclass UIImageView and override methods setFrame and setBounds to met your needs.
I have to integrate a UITableViewCell generated programmatically from code like so:
UITableViewCell *newCell = [[UITableViewCell alloc] initWithFrame: CGRectMake(0,0, screenWidth, 150];
However the rest of the cells in this table view are generated with xibs that rely on autolayout, and so the original programmer only used the estimatedHeightForRow method rather than the heightForRow.
My programmatically generated table view cell is all botched (has the default estimated height of 64 rather than the frame's height of 150) unless I implement heightForRow whereas the existing cells are botched as soon as I do implement heightForRow in addition to estimatedHeightForRow. Is there a way around this conundrum?
Thanks for any advice.
Firstly, you should use UITableViewCell's designated initializer - initWithStyle:reuseIdentifier: instead the UIView's initWithFrame:. However, I don't think that is the cause of your problem as I have tested initWithFrame: and it appears to default to default style and no reuse identifier.
You haven't said what you are doing with the cell once you instantiate it. Are you using the default layout or adding your own custom views? The default layout (with just a line of text) appears to work correctly in my testing, with the cells being sized according to the amount of text in the textLabel.
If you are adding custom views you need to ensure that the contentView has vertical constraints that fully define its height. For example, if you have two UILabels vertically aligned then you will need a vertical constraints such as #"V:|-10-[label1][label2]-10-|". If the constraints are not fully defined then contentView will collapse to zero and your views will appear overlapping the next cell.
To give a little more detail, when using self-sizing cells UITableView does not look at the frame of the returned cell but rather calls the cell's systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:. This results in the Auto Layout engine analyzing the cell's constraints to calculate the appropriate layout size. UITableView then sets the cell's frame according to that size and its position in the table.
I would put an if statement in your height for row method and set the heights to their corresponding cell. It would also be easier if I could see code.
I'm experimenting with autolayout and am running into trouble with UITableViewCell since they're created at runtime. My cells are loaded from a xib from the main ViewController. This xib has View mode set to Aspect Fill.
I've read about different ways to do this online and have yet to get any of them working. What's considered the best way to handle this?
It looks like your constraints aren't set properly, as the cell is shorter than the image's height.
Using AutoLayout and self-sizing cells is the easiest way to handle what you want to do. Once your constraints are setup properly for your custom cell, tableView:cellForRowAtIndexPath: can call dequeueReusableCellWithIdentifier:forIndexPath: and all the subview layout will be handled for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He also provides workarounds for the minor issue with the initial cell width being based on the storyboard cell, instead of the tableView width. I worked around it by setting the cell's initial width to the tableView's width, as Rasputin had suggested.
I have a collectionView inside a UITableViewCell. The collectionView's content is dynamic, hence, its height is dynamic too and the tableView cell's height depends on the collectionView's height. The collectionView cells have different sizes. The collectionView's height is always set to fit its contentSize height.
My problem is that the tableView's function heigthForRowAtIndexPath, is called before the collectionView is created (in cellForRowAtIndexPath) so, to return the height that fits the collectionView, I need to calculate it manually (which doesn't seem like a good idea for a collectionView with cells of different sizes).
I tried to use autolayout in order to use UITableViewAutomaticDimension but it didn't work (maybe I did it in a wrong way).
What is the best aproach to make a UITableViewCell consider the height of its subview in heightForRowAtIndexPath? Can I know a collectionView's estimated size without creating it?
Use self sizing, which is available in iOS 8. There are plenty of good tutorials online, like this one: http://www.appcoda.com/self-sizing-cells/.
The idea is that you can use auto layout and a few lines of code in viewDidLoad to render a table view cell that dynamically fits the content in it.
Some more tutorials:
http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html
https://github.com/smileyborg/TableViewCellWithAutoLayoutiOS8
I'm new to iOS development and am stucked with the problem to finish my layout with dynamic content..
Generally layout I'm trying to implement is quite popular, as an example:
and here is screenshot of my storyboard:
and table cell hierarchy:
The main question what are the main rules of building dynamic height table cell with dynamic height uiview inside it? The content could be long, so do I need to add constraints to bottom of the view?
Is it possible in Storyboard?
Thanks!
I don't think it is possible with storyboard (I might be wrong...): UITableView can have cells with a fixed size - using UITableView rowHeight property - or its delegate can provide a different size for any indexPath.
Constraints provided in Storyboard (or programmatically) only help layout the cell's subviews ONCE the cell's frame has been set.
So I think you should look to tableView:heightForRowAtIndexPath:, and a way to compute programmatically your cells' height dynamically.