UITableViewCell automatic dimension is not working well - swift

we are displaying some data in custom tableview cells but if some labels are empty we are hiding the labels and using uitableview.automaticdimension in estimated row height but not working well.Thanks in advance
image that how i got my tablecell
declared in
func viewDidLoad() {
self.cabinetVC.estimatedRowHeight = 260
self.cabinetVC.rowHeight = UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
}

Automatic dimension relies on Auto Layout to determine the height of the cell. That means that there must be non ambiguous constraints. Make sure you have constraints from the top of the cell to the bottom of the cell.
For example:
---------------
|
---------
| label |
---------
|
---------------

You should comment or remove this delegate method,
/*func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}*/
You are already providing some static value in viewDidLoad, that will helps to the proper sizing of the tableView cell.
Try and share the results.
EDIT:
For 3x3 labels in a cell, you can do it as follows:
1) Take a vertical stackView
2) Add 3 Horizontal stackView to it.
3) Add 3 Labels to each horizontal stackView
4) Set numberOfLines to zero
5) Set equalWidth constraints to all inner stackViews and Labels.
6) Set leading, top, trailing and bottom to all stackViews and Labels.
For the label, you don't have the text - set nil to it and it will be handled properly.

Related

Swift tableviewcell height stays fixed

My tableviewcell height stays fixed even though I do the following:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 150
I have one imageview and one label inside the cell and I gave constraints to each side.
Also, I tried:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
I need a fixed height for my cell. It doesnt need to be dynamic. But no matter what i tried the height stays around 50px.
Any help?
Setting automaticDimension is based on using auto layout in your cells.
This means you must add a NSLayoutConstraint to all sides of a UILabel (or any other control you use in your cells) and the UITableViewCell container in interface designer or in code. In case of a label, configure setting 'Lines' to 0 and 'Line Break' to Word Wrap.
you can set the height you want with heightForRowAtIndexPath, hopefully that helps

How to set constraints for Xib for dynamic height of a cell...?

I am using xib to display the data but if the data for labels are empty am hiding the labels but height of the cell is not decreasing based on the contents.Thanks in advance your suggestions are helpful
You should use auto layout constarits,
not set a fixed height for labels please connect to top and bottom
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

Setting the height: TableViewCells with custom Nib that are different sizes

I have a TableViewController that has a custom TableViewCell under the identifier "customCell". Heres an image of the configuration of the cell along with the IBOulets connected to it:
The cell takes information from my backend and presents it. The description text view (just noticed that I accidentally named it descriptionLabel) doesn't allow scrolling so it expands based off of the content that it's holding. The database is being processed correctly from the database, and it's displaying on the app. The only problem is that the cell is not its correct height. On the TableViewControl that's registering this cell through its identifier, I automatically set the height of the cell using UITableViewAutomaticDimension in heightForRow:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
but that's not working either. *The only thing that works is when I set the height of each cell to a standard value such as 200. That doesn't work for me because each cell will be a different height because of the length of the textView.
How do I set the height of a custom nib (tableViewCell) so that it adjusts based off of the content within it instead of setting the height to a specific value?
1-Remove both textViews and replace them with labels
2- Title lbl with theses constraints
top,leading,trailing , .lines = 0
3- Description lbl with theses constraints
bottom ,leading,trailing to contentView,top to bottom of title lbl , .lines = 0
Off course you can leave the 2 textviews , but you have to give each one an initial height and do this in
override func layoutSubviews() {
super.layoutSubviews()
self.titleTvHeight.constant = self.titleTv.contentSize.height
self.desTVheight.constant = self.desTv.contentSize.height
}
//
Don't forget to set this in viewDidLoad of the VC
tableView.estimatedRowHeight = 200
tableView.rowHeight = UITableViewAutomaticDimension
Anyway you can remove heightForRowAt

in UITableviewcell in Xcode 9.0.1 cell size it not expanding at runtime?

I have tried with drag and drop UITableviewcell and change row height from file inspector cannot expanded cell height at runtime.
in Xcode 9.0.1 I am facing issue regarding Cell height not increasing at run time.
Swift 3+
If you want dynamic row height, you can define your constraints (making sure they're unambiguous), set the label's numberOfLines to zero, and then in viewDidLoad, tell it that rows should automatically adjust their height:
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = your height here
tableView.rowHeight = UITableViewAutomaticDimension
}
OR Try this.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You have to configure your cells to be self-sizing:
1: Add the following lines to viewDidLoad(). Specify a tableViewRowHieght that you think will be the average height of the cells displayd in your tableView:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 200
2: Makes sure all of the labels in your cell are capable of expanding to as many lines as needed. This is done by setting Number Of Lines in Attribute Inspector to 0 as I have done for my Title label in the image below:
3: Finally, you must set constraints for every element in your cell so that the compiler can correctly calculate the size of the cell at runtime. Here I've added two lables contrained the height of the first to the top of the view, and bottom to label 2 below. Then label 2 must be contrained to the bottom of the view. As long as your contraints contain no "gaps" between the top of the cell to the bottom, the compiler can calculate the height of the cell:

Swift UITableViewAutomaticDimension is not working

I have got a table in my Swift project like this
var tableView: UITableView!
tableView = UITableView()
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = UITableViewAutomaticDimension
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postTexts.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 250;
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 5
}
I wanted my tableview's height to be automatic thus i used
UITableViewAutomaticDimension,but it is still showing nearly half of the cells width
You need intrinsic cell size for this to work. Delete heightForRowAt entirely. Your constraints in your cell should determine the cell size, this means your constraints should be at least pinned from top and bottom. Add the following code after setting the rowHeight:
tableView.estimatedRowHeight = 250
You must delete the -heightForRow method and set on the table view an estimatedRowHeight.
The estimatedRowHeight is used to calculate the bar size etc of the TV, so it should be a real value closest as possible to an average size of the table view cell height.
Now, if you are using auto layout and constraints are set correctly you should see a correct resize of your UITableViewCell. To set correctly the constraints inside the TVC you should think to place them in a way that they can control the size of your cell.
For instance say that your TVC has just one label and that label has 4 constraints attached to its superview: top, bottom, trailing and leading with a fixed constant size. The UILabel instance has also the numberOfLines set to 0 (means that it can expand to fill all your text).
When the autolayout engine starts to request the label intrinsicContentSize, the label will return a value that will fit all your text, the superview size will change the size according to that.
If you fix the TVC to a specific height as you did before, the TVC can't expand itself.
I had problems with constraints so here is what i did.
First i added heightForRowAt
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
Then i created a variable
let tableViewHeight = postTexts.count*100
And put that in my height constraint
tableView.anchor(PostsDiv.bottomAnchor, left:view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 40, leftConstant: 15, bottomConstant: 0, rightConstant: 15, widthConstant: 0,heightConstant: CGFloat(tableViewHeight))
And it works pretty well for me
Pin the view's constraints to the superview's edges. Also call the estimatedRowHeight.. Remember to call a reload from the table to invoke height calculation for each cell to display.