Change custom UITableViewCell height based on view - ios

I have a custom UITableViewCell I've created programmatically that I want to be the full height of my view. I've overridden "heightForRow", but that doesn't affect the size of the cell's frame on the first load. The current frame loads at 320 width, 44 height which I guess is the default size for a cell. The frame of the cell does not change until I actually scroll the cell off screen then back on to "refresh" it. What should I do to set the cell's frame height to be full screen on the first load?

Reload tableview in viewDidAppear. Best way to handle these kind of scenarios is making cell through xib or storyboard and applying auto layout constraints.

Related

iOS custom view inside table view cell with intrinsic height

When UILabel with multi-line and wrap-word enabled is used on UITableViewCell, UITableView handles the different cell heights without any problem. UILabel calculates its height according to its content and its width.
I implemented a custom view (https://github.com/fthdgn/ios-demo/blob/master/demo/CustomGridView.swift) which also calculates its height according to its content and its width. This view is a simple grid view which determines row count and height according to column count which is determined by available width. However, there are problems when custom view is used on UITableViewCell.
On my demo application (https://github.com/fthdgn/ios-demo),
On the 1st tab [1st image], the custom view is used alone. Its height is correct. Button adds new items and updates it height correctly.
On the 2nd tab [2nd image], the height of the custom view is calculated wrongly. This height is calculated according to placeholder width of the view on the XIB file. This width is smaller than correct width, thus the height is longer than correct value.
Reload button on the 2nd tab [3rd image], reloads table. Custom view calculates and updates itself correctly.
On the 3rd tab [4th image], UILabel calculates its height according to available width from the start.
Why does not my custom view work like UILabel? Are there missing setNeedsLayout, invalidateIntrinsicContentSize calls?
I updated the code to show only 1 row with 11 cells on UITableView.
The width of Grid View on on XIB file is 300, because the selected device is 4S. The simulator I use is 11 Pro Max, it is wider than 4S.
intrinsicContentSize and layoutSubviews call prints these:
content (-1.0, 50.0) frame (300.0, 0.5)
First calls intrinsicContentSize. Frame is what is on the XIB,
constraints does not resized the view yet. This width requires 50
height to place cells.
layout (394.0, 50.333333333333336)
Calls layout, the height is determined according to intrinsicContentSize, width determined by UITableViewCell constraints. It is wider then the value which intrinsicContentSize is calculated by.
content (-1.0, 20.0) frame (394.0, 50.333333333333336)
Because layout changed, I invalidate the intrinsicContentSize. New intrinsicContentSize value is calculated according to correct width. New height is 20.
layout (394.0, 50.333333333333336)
Something triggers layout. Eventhough last intrinsicContentSize.height is 20, it still uses 50.
content (-1.0, 20.0) frame (394.0, 50.333333333333336)
Layout invalidates intrinsicContentSize. intrinsicContentSize is recalculated but its value does not change.
After these calls, event though the last intrinsicContentSize wants 20 height, frame continues to be 50. You can see at 2nd screenshot, the row with 11 cell is longer than what it should be.

How to set Multiple Size/Merge cells in UICollection View

I want to make a layout using UICollectionVIew. But I am not sure it is possible or not.
I have tried changing height of one Cell but it is not working. Here is the layout which I want to acheive:
In this Layout height of each cell is fixed only the height and width of first cell is different.
How can I achieve this.

UITableViewCell not resizing for UIView subview

I added this library called KSTokenView which is a tokenizer like NSTokenField. This UIView subclass was added to the UITableViewCell. Connections were made with all the 4 sides of this UIView to the content view. Now as I type into the view and the size changes, there is a delegate method for change of frame.
Using the frame change for that, I need to resize my tableviewcell and in turn the tableview also.
In the view controller for the table view, I am giving an estimated row height of UITableViewAutomaticDimension. I am currently doing tableView.beginUpdates and tableView.endUpdates on change of frame.
What is the correct way?
I think changing the frame is not the right way to do it (if you're using autolayout). You can set a height constraint to your view and update its constant instead, in the same place you update the frame.
Update
I am giving an estimated row height of UITableViewAutomaticDimension
You shouldn't set estimatedRowHeight to be UITableViewAutomaticDimension. It should be an estimated average number of your cell height and rowHeight should be UITableViewAutomaticDimension.

UICollectionView cells not showing when I set the height too low

I'm trying to add a UICollectionView to my view to use as a scrolling date picker on a single line. I'd like this to be relatively compact, but I'm having trouble when setting the collection view's height in IB.
Basically if I drop the height of the frame below 114, my prototype cells disappear, and no cells are displayed when I run the app. As long as it's above that value, everything works fine.
I've also got a black bar of empty background space above the cells despite setting their height to the same as the frame's height in sizeForItemAtIndexPath.
My delegate and datasource are set up correctly, and my cell has an identifier set and is being dequeued just fine, as long as the view's height is large enough. Do I need to subclass UICollectionViewLayout to get a really short view to work?
Uncheck Adjusts Scroll View Insets on your view controller or set automaticallyAdjustsScrollViewInsets property on the view controller to false programmatically.

With automatic UITableViewCell height in iOS 8, is it possible for it to work if the cell's contents are embedded in a UIView?

My UITableViewCell subclass has several UILabels in it of varying length, and with iOS 8 and Auto Layout constraints in the cell the height is now automatically configured and cell heights can be dynamic.
However I want to embed all the contents of the cell in another UIView. Is it possible for the cell's height to still be dynamic? As in the UIView that the cell contents are embedded in will grow and shrink automatically based on the content and thus the cell will as well? Is this possible?
Dynamic height works by performing an Autolayout pass on the cell's content view. As long as the constraints are in place for a height to be calculated, the specific view hierarchy should be irrelevant.

Resources