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.
Related
I am trying to add table view that is contained into a view, this view has a width constraint and the height of the view will be dictated by the content height of the table view. However, I am unable to get this to work.
My attempt:
I create a view with a leading, top and width constraint. Then I insert a table view inside this view, this table view will have a leading, top, bottom and trailing constraint to the view (it's superview). I also set the row height and the estimate. When running the code, numberOfRowsInSection gets called but cellForRowAt does not, my guess is because the height didn't solve properly and the table has a height of 0. How can I fix this?
Thanks.
luis,
UITableView inherits from UIScrollView. So You cant expect the tableView to provide the height for its superView. In fact its vice versa.
TableView's frame will be decided by the superView's frame where as the tableView's contentSize will be decided by the data that you have provided.
So your best bets
Solution 1:
Add a height constraint to the View (SuperView) and create a IBOutlet to it in your VC.
Set the height for each cell in tableView lets say as 50.
Once you get the data to show in tableView before calling reloadData() on tableView set the constant of View's height constraint as
viewsHeightConstraint.constant = (numberOfRows) * 50;
yourView.layoutIfNeeded()
Solution 2:
If you are planning to use automatic height for cell then set the height of view based on the ContentSize of tableView. But to do that you have to get the data first, reload the tableView with the data and once done now get the content size of TableView and set the frame of your view accordingly.
I've got a UITableViewCell subclass that has two labels of variable height. AFAIK, the autolayout constraints are correct. The first time the cell is rendered in the table it's correct, but any subsequent rendering is blown out too tall. Even after the parent view controller is deallocated! It literally requires an app restart to go back to normal.
I've tried clearing text of the labels in an override of prepareForReuse in the cell class.
// viewDidLoad
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
To support dynamic layout with textview, a height constraint is needed and that needs to be updated in layoutSubviews method(you need to subclass UITextview).
In addition to this, you need to make sure you have a UITextview bottom constraint with cell's contentView.
Sounds like your bottom constraint of UILabel is getting increased. Check if you are updating the bottom constraint constant value.
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.
I am using autolayout to determine the cellsize (fixed with, variable height) and set all the necessary constraints in the storyboard. Autolayout works fine as long as I don't change the constraints.
But at Runtime I have to add buttons to some cells. I remove the bottom constraint of the last label, insert the button and add a constraint to the label above, one leading constraint to the cell and one to the bottom of the cell.
The problem is, that the cells size is not updated after this. I tried to call LayoutIfNeeded in the cell and in the collection view but that did not work. I think the constraints are set up correctly. The button appears at the right position but the cell just keeps the height for a cell without a button. How can I tell the CollectionView to update the cell sizes?
You could refer the below link probably you can get your answer with manually and automatically size of UICollectionViewCell.
How make a collectionViewCell auto size only by height?
==> You can either use the UICollectionViewFlowLayout method itemSize property to set the UICollectionViewCell size, or use the delegate method, collectionView:layout:sizeForItemAtIndexPath: if you need to dynamically set the sizes of the UICollectionViewCells.
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.