Autosizing UITableViewCell: change height of a view inside a custom cell - ios

I searched a lot for a solution but can't find anything to solve this particular case.
I created a custom cell in a UITableView, with these elements in order from top with the constraints:
imageview (constraints top screen, fixed width, fixed height, centered horizontal)
top-label (constraints top imageview, fixed width, centered horizontal) Line set to 0.
view (constraints top label, fixed width, centered horizontal) The height is set to 0.
button (constraints top view, fixed width, fixed height, centered horizontal)
bottom-label (constraints top view, constraints bottom screen, fixed width, fixed height, centered horizontal)
The cell is autosized in height correctly, also if I insert a text for top-label very long.
Now I want to attach an action to the button to enable the resize in height of the view, like an accordion. So I'm trying to change the height with no success, anything change also if I reload the tableview.
I tried to set a constraints to the height of the view, but If I change that all the content move but the height of the cell doesn't change.
The only way I can have the view changing the height is setting the the rowheight of the table, but that change all the cells of the table and I want to open the view of only one cell.
Is there another way to do that?
Thanks in advance
Ale
EDIT:
For clearance I've solved using the delegate method suggested by noobular, setting the view to 0 height permit to have that expanded when the height of the cell change.

I achieved a similar effect by these methods. First, I determine a constraint that will represent the height of my cell. I'm using the height constraint of a decorative bar on the left hand side that's pinned to the top and bottom of the cell. It's important that this constraint belong to an object that is a child of the cell's content view and that will be on-screen in both open and closed states. This constraint is connected to a property on the cell it will control.
When I want to change the height of the cell, i.e. to 100, I can do so as follows:
cell.heightConstraint.constant = 100.0
cell.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
The calls to beginUpdates and endUpdates force the tableView to layout its cells again, thus expanding or contracting the cell. Note that I'm actually calling the first two lines in the cell's own setSelected:animated: function and the remaining two in the table view controller's tableView:didSelectCellAtIndexPath: method. This has the effect of expanding a cell when it is selected.

Edit: For auto sizing cells where you want UIKit to automatically calculate the height, I would suggest researching UITableViewAutomaticDimension
Implement this method for your tableView delegate:
tableView:heightForRowAtIndexPath:

Related

How to set UITableView cells width to same as its parent/device width?

I have a UITableView cells that won't follow it's parent width. I found so many codes for how to change the height, however I couldn't found codes/ways to change UITableView cells to same as it parents.
Some of answer told that it would follow automatically. But in mine, the problem is the width can't follow device's width. Is there anyone that know how to change it?
The UITableviewCell width will always be the same width of the UITableView.
If you want to set the width of the cell same as the parent of UITableView, You should set the width of the UITableView same as that of parent
If you want to set the width of UITableView the same as it's parent view, set the trailing and leading constraints of your UITableView to it's superview with value 0
you need to remove the width constraint and aspect ratio that you have added to it. Instead of ctrl and drag, select the tableview and click the pin button and set the constraints as shown in image. Remember the trailing and leading values must be 0 for setting the width as same as parent view

Constraints ambiguously suggest a height of zero for tableview cell's content view

This warning is driving me crazy:
[Warning] Warning once only: Detected a case where constraints
ambiguously suggest a height of zero for a tableview cell's content
view. We're considering the collapse unintentional and using standard
height instead.
I have a UICollectionView inside a UITableViewCell. The table view cell has auto row height, and the collection view has cell size of 44x44. The collection view cell has default size. I have a button inside the collection view cell with constraints pinning it to the top, bottom, left, and right of the cell (I also tried manually setting its size to 44x44). You can see the rest of the constraints in the following image:
When I try to set the Table View Cell Row Height as custom equal to 44, I get an error that the constraints are conflicting and one of them needs to be removed. When I set it to 50, I get the warning above again.
Any ideas? :S
Thanks!
make sure in your cell class to define constraints in a such way that cell contentView will have a least 1px height/width (e.g. height constraint >= 1)
I do like this (collectionView is a subview of cells contentView)
collectionView.heightAnchor.constraint(greaterThanOrEqualToConstant: 1).isActive = true
CollectionView constraints are wrong, you added top constraint and also aligning collectionView center vertically, remove one of them. also take a look of screen short.

Make custom cell's ( created with autolayout) height to zero

I want to make height of a custom tableview cell to zero, which was created with autolayout. Basically I have container view added as subview to cell.contentview. Container view's top, bottom, left and right are pinned to content view's top, bottom, left and right. Under certain condition I want to make height as 0.
I iterated through content views's constraints and found this constrain causing the problem.
NSLayoutConstraint:0x1369d8670 UITableViewCellContentView:0x1369b1a90.height == 48
This constraint is added by default by tableview.
Things I Tried : I removed all subviews of container view. But this does not make cell height to zero.
Things I don't want to do:
I dont want to reload entire tableview coz I will be having many rows with many subviews.
I don't want to delete row
How can I set height to zero?
You can reload particular row so it will call tableview's delegate method for height change heightForRorAtIndexpath. You can set the height 1 when you programmatically reload particular row. You can store temporary indexpath which you want to make zero.
Clip all the subiviews by clipsToBounds=YES
call [self.tableView layoutIfNeeded];

How do I change cell height according to its content?

I have a Detail View Controller, which has different text lengths according to which cell you press in the Main View Controller, of which it segues to. How do I set the cell's height according to how much content is in it?
FYI, the cell consists of 3 labels and 1 image view. It is vertically set as UILabel, UIImageView, UILabel, UILabel.
I am using Swift.
This is what happens when I add the auto-layout constraints:
The way to do it is to use auto-layout. You need to set up vertical constraints from the top to the bottom of your cell:
Distance between the top edge of the cell and the top edge of the first label.
Distance from the bottom edge of the first label to the top edge of the image view.
Distance from the bottom edge of the image view to the top edge of the second label.
and so on ...
Auto-layout will take care of sizing the cell depending on the size of the content you put in your labels and the image view.
You should also remember to set the estimatedRowHeight property on your table view to some meaningful value. This will help the table view defer some of the calculations of the content size to when the user starts to scroll.
Also, set the rowHeight property on your table view to UITableViewAutomaticDimension.
There are 4 things you'll need to do:
Set your tableView:heightForRowAtIndexPath method to return UITableViewAutomaticDimension.
Set your tableView:estimatedHeightForRowAtIndexPath to return the average height of the cells. You can set it to something static like 100.
Set your Auto Layout constraints so that all subviews are tied to each other vertically, and the top-most and bottom-most views are tied up to the cell's contentView.
Set the label's preferredMaxLayoutWidth property either in code or in your Storyboard to be equal to the label's width.
To fix this, I added the sizeToFit() property to my label.

Create UITableViewCell with greater width than its table

I have a custom UITableViewCell that its width is larger than UITableView's one (>1500px), user can scroll horizontally to show the whole cell.
I tried to set the contentSize.width of the table to match my cell's width but when I scroll the table view to the left, and when I reach a point higher than screen width, all information disappear and table become white.
You simply have to use a UIScrollView inside each UITableViewCell.
I ended up putting the table view inside UIScrollView and set its width to match maximum UITableViewCell width, then set the content width of the UIScrollView to match this width, now I can scroll the table horizontally without any problem, thanks.

Resources