Swift, dynamic table view cell heights using Storyboards - uitableview

I've been wrestling with this for a better part of a week and I'd love some help.
I have a cell laid out in storyboards that's a subclass of UITableViewCell called EntryCell. I've been using autolayout to set constraints in storyboards and all of the constraints on all the elements are blue, lldb throws no errors at runtime so nothing is duplicated or out of place.
My label that says "Journal Entry Here" below is a UILabel and has lines set to 0.
In my view controller under ViewDidLoad, I have
tableView.estimatedRowHeight = 185
tableView.rowHeight = UITableViewAutomaticDimension
but when I run that, simulator shows this:
This is holding up my entire project. I've read a number of tutorials and tried their implementations to no avail. I'm currently not doing anything with heightForRowAtIndexPath. I wonder if that's the problem or if it's something else.
Any help would be hugely appreciated. Thank you!

You shouldn't have to use heightForRowAtIndexPath to get this to work. The main reason UITableViewAutomaticDimension won't expand to fit unlimited line labels is because of missing layout constraints--constraints that won't necessarily show up in the console as errors. There must be continuous anchoring for every UI Object from the top to the bottom and to the left and right of your cell for expansion to work properly. If you think you've taken care of this and the cell still doesn't expand, try posting a screen shot and list of your layout constraints so we can review.

Related

Why might Autolayout constraints not appear at runtime?

I am trying to create a custom subclass of UITableViewCell that displays an image and a few labels to the right of the image. I have my UIImageView and UILabels arranged in stack views, the outermost of which I pinned to all four sides of the content view.
But when I run the app in the simulator, I do not see the image views. Debugging the view hierarchy, I see they are getting clipped.
And digging in a little further by focusing on the outermost stack view with "Show Constraints" on, I don't see my auto layout constraints at all (instead I get an ambiguous position runtime warning for each of the outermost stack views).
The text label that expands to the right is set up with 0 lines and the table view gets a constant estimatedRowHeight value and its rowHeight set to UITableViewAutomaticDimension, so I think if these constraints were working, the rows in the table would size to fit the content. Perhaps notably, any width or height auto layout constraint I add does appear when I debug the view hierarchy, so I know some constraints are present at runtime, I just cannot for the life of me figure out why the constraints relative to the content view are not.
After wrestling with this for a long time and having a much more experienced developer look over my project, it seems as if there was nothing wrong with the way it was set up. Recreating the table view cell from scratch with exactly the same layout and constraints and using the exact same code fixed this issue. I had messed around with the stack views and constraints quite a bit before this started happening, so it might be possible to get Xcode into a place where it does not properly set up constraints when you run the app in the simulator.
Try to solve all warnings which are printed into your console log when you run your application and viewing this screen. Because you can not ignore warnings always, many warnings you must have to remove by correcting or solving issues in your constraint. And that is the only solution which helps you most of the time.
I'm 5 years late to the party, but I managed to find a solution to this problem in my project. If any of the views that are contained in the table view cell have a bad subclass set on them, the intializer defaults them back to a regular UIView and without any of the layout constraints. Hopefully that saves someone the hours it took me!

UICollectionView Different Item Height Issue

Keypoints:
I am using XIB with Autolayout
I am trying to create a UICollectionView that has a dynamic item height. This is needed because I have an UILabel that might have different heights due to it's length. The UILabel's text is set using it's attributedText property
I am aware that:
I MUST NOT implement sizeForItemAtIndexpath.
I MUST set estimatedItemSize to the flowLayout of my UICollectionView.
I have been scavenging Stackoverflow for more than 6 hours but still unable to find solution but most of the solutions are either emphasizing on Storyboards or intended for lower version of iOS.
The best one i have found so far is this UICollectionView Self Sizing Cells with Auto Layout
However, that also did not fix my problem because somehow, my cell does not have the intended width.
Any ideas please?
.
.
Please help me vote this up cause I am literally banging my head now. Any help is very welcome.

swift uitableview in uitableviewcell with auto layout always height of 0

I have a UITableView defined in a storyboard with a UITableViewCell that contains another UITableView as part of its content with a UITableViewCell defined in it all in a storyboard.
IB image
Both content views are fully specified by auto layout. The nested table however always has a frame height of zero. If I turn on a height for the nested table you can see all of the content so I know it is there; however without the hard coded height its always 0. I've done this before but for whatever reason I am missing something.
The top level table works fine. The nested table properly loads its cells. I've done the trick of
removalTable.estimatedRowHeight = 127
removalTable.rowHeight = UITableViewAutomaticDimension
removalTable.setNeedsLayout()
removalTable.reloadData()
I'm not really new to SO but for whatever reason I can't get my openID to work.
I have seen this error a lot of times.
it is probably because you are not setting good constraints inside the cell you are displaying. What you need to check?
you have an anchor to the top and the bottom at least.
you have a fixed height in one view of your content.
Maybe this post can help you: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
I hope it helps you.

Two uilabels on uitable with dynamic height

I am trying to implement one English to Swedish learning app for Swift/IOS there I want to have two dynamic rows, data is coming from array and I tried my best to fix it out. I have seen approx. all stack overflow links as well but no success at all.
See the screenshot, as the text is long, I cannot see it on the screen and the text is not coming on the second line as well. As, I add any constraint, both lines sometimes disappeared or only one lines comes.
I have seen this tutorial link as well but cannot get any success.
Can any please see the code and fix it.
Here is the complete code
You have to use UITableViewAutomaticDimension to manage the cell height as per the content. For that you have to set the constraint of your labels from all the sides, ie Click on the PIN and then uncheck the 'Constraint from margin' check box and for both of your labels add constraints from Top, bottom, Left and Right side.As it is required if you want to use UITableViewAutomaticDimension.
Now in the ViewController in viewDidLoad method add this two line
self.tableView.estimatedRowHeight = 44.0
self.tableView.rowHeight = UITableViewAutomaticDimension
and then you your labels will display full text.

Vertically scrolling UICollectionView with self-sizing cells only displays half the cells

I've set up a UICollectionView (with the default flow layout, and vertical scrolling) with custom cells that load from a xib. The cells contain a couple of elements (a UILabel and a UIView) with constraints on them. I've set up the constraints such that the cell height grows as the label text height increases using the new UICollectionViewFlowLayout property that's available in iOS8, estimatedItemSize:
layout.estimatedItemSize = CGSizeMake(self.collectionView.frame.width, 100)
Everything works like a charm except there's one big problem: the UICollectionView loads only half the items as returned by the numberOfItemsInSection method. So, when the method returns, say, 10, my UICollectionView only displays 5 cells (but displays and lays them out perfectly).
Some relevant findings from my debugging attempts:
I've been able to force the loading of the remaining items by calling invalidateLayout or changing the number of sections from 1 to 2. But those are just debugging hacks.
Separately, everything works like a charm when I replace estimatedItemSize with the itemSize property, i.e. hard-coding the item size. But that defeats the self-sizing functionality I'd like to implement.
I'm assuming there's something wrong with how I'm thinking about self-sized cells. Specifically, I wonder if the problem has something to do with constraints.
I'd appreciate any help here.
I believe this is due to a bug in iOS8 which still exists as of iOS8.3.
Flow layout collection views with self-sizing cells will fail to display some of their content if the estimatedItemSize is too small. This is despite the fact that according to the the API docs the only requirement on the estimatedItemSize is that it be non-zero to trigger the self-sizing behavior.
Only Apple is in a position to fix this. I think the best workaround in the mean time is to put an inflated value into estimatedItemSize`.
This repo demonstrates the issue: https://github.com/algal/SelfSizingCellsDemo. (I've filed a radar at rdar://18078598.)
The obvious non-hacky solution is to implement
flowLayout.collectionView(layout:sizeForItemAtIndexPath:)
but of course that is more code than you presumably have hoped for.
I would suggest you try two more things:
Try to experiment with more plausible estimated sizes. Also try the default value, CGSizeZero.
Check what happens in preferredlLayoutAttributesFittingAttributes() by examining the layoutAttributes parameter. Maybe you find a clue to why the other cells are "crowded out".
In a custom subclass of UICollectionViewFlowLayout
override prepareLayout like so:
- (void)prepareLayout {
[super prepareLayout];
CGSize contentSize = self.collectionView.contentSize;
// Without this line, the scrolling doesn't go all the way to the bottom, right away but takes a while
[super layoutAttributesForElementsInRect:CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height)];
}
I had this exact same problem.
I was using layout.estimatedItemSize = CGSizeMake(373.0, 500.0) together with
preferredLayoutAttributesFittingAttributes( layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes!
and I got exactly the self-sizing behaviour i wanted, except I was only getting half the cells displayed.
The curious thing is when I changed the estimatedItemSize property to: layout.estimatedItemSize = view.bounds.size ALL the cells appear!!
Hope this helps. Good luck.

Resources