Custom UITableViewCells overlaps in table - ios

I have a custom cell:
But when tableView loads i've got overlaping cells in it:
I've tried to insert the following code into my viewDidLoad():
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableViewAutomaticDimension
But it does'not helps.

From the docs:
//Declaration
//SWIFT
optional func tableView(_ tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
//OBJECTIVE-C
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
Make the method return the height of the cells. If this does not help, then you need to give us more details (and constraints probably).
EDIT:
I managed to fix the issue by doing 2 things:
Change those two lines:
tableView.estimatedRowHeight = 150
tableView.rowHeight = UITableViewAutomaticDimension
The tableview will automatically make the cells smaller
In the cell xib, you should add the constraint to the bottom of the cell. This way the height can be properly calculated.
If you want I can send you a working copy of the project, just give some contact mail or sth.

When you do this:
tableView.estimatedRowHeight = tableView.rowHeight
Assuming that tableView is a UITableView, here you need to give a number, not the UITableView rowHeight property (something like 100).
Here you give an estimated height just to let the phone calculate the total height of the table, it is not the real height of each cell. So give the average heigh you estimate.
The tableView.rowHeight = UITableViewAutomaticDimension is ok.
And also review the constraints in the cell view. To get this work you have to have constraints from the top to the bottom, and in the middle of the labels.
Edit: I've seen that the last constraint is missing (a bottom constraint for Date label)
Now, seeing the code, I'll say that this method is not neccesary:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Hope it helps.

Related

UITableView Automatic Dimension Not Working Correctly

I have a tableview that is getting populated with data from Firebase.
However, when resizing the tableview using automatic dimension, some text is shown getting cut off.
Here is my Storyboard with constraints set to top, bottom, right, and left.
It is working fine when there is not alot of text as shown here.
However, when I fill the cell with alot of text this occurs.
Here is the code that I am currently using.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (tableView == questInformationTableView) {
return 50
}
else if (tableView == questWalkthroughTableView) {
return UITableView.automaticDimension
}
return 0
}
override func viewDidLoad() {
super.viewDidLoad()
questWalkthroughTableView.estimatedRowHeight = 250
questWalkthroughTableView.rowHeight = UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
I have also set numberOfLines to 0 and set the label to .sizeToFit()
Don't make the bottom constraint greater than or equal to just set it like the top constraint. Take away the estimatedHeightForRowAt and heightForRowAt functions. Your view did load declarations are sufficient. When you reload data also call self.view.layoutIfNeeded
I usually set the automatic height dimension and then do a reload in the viewDidLoad like this
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
tableView.reloadData()
}
Suggest to check
automatic row height is enabled inside the table cell
check if the label is expandable and its bottom constraint is set to superview.
Adding screenshot showing list of constraints of a sample cell I am using with dynamic height.
Even if this doesn't fix your issue, suggest to reset all constraints and set it again for your cell.
For a similar issue, I removed the tableView.estimatedRowHeight assignment and kept tableView.rowHeight = UITableView.automaticDimension and everything started working. Only seemed to be an issue on iPad though.

how to increase the label size according to text in static table view in swift 3

I have 3 custom cells in Static TableView. All cells have different heights. But I need to make height of newsDetail label dynamic according to its content size. It was easy to make dynamic for dynamic tableview by setting rowHeight and estimatedRowHeight. But it didn't work out for Static TableView. I searched but couldn't find solution for this, anyone please could help me in swift 3?
If you are using Autolayout you need to use,
In viewDidLoad() provide estimated height
tableView.estimatedRowHeight = 60.0 //you can provide any
tableView.rowHeight = UITableViewAutomaticDimension
Implement Delegate method
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension;
}
Need to provide constraint on your label in UITableViewCell.
Top, Bottom, Trailing, Leading and no need to provide any fix height constraint.
And make numberOfLines to 0 of UILabel

I need to set UITableViewCell Dynamic Height, With Multiple dynamic label with UIImageView

I need to set UITableViewCell dynamic height based on it's content.
Here is my demo image:
In this to label which have dynamic content so based on that label content my cell height should change..
I get the constraint error. In this cell two UILabel is available and both have dynamic content size. (I already make the demo with auto resizing cell with only one label.) But in this cell two label are there. So error is xcode suggest me to give Y POS or Height to label, but I already set top constraints. (4 side constraints with label line set to 0 Zero) But still it's fails.
I also try with this code
self.tableView.estimatedRowHeight = 250.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
Here is my demo code
Look at the screenshot of my output. For both the labels , you should give all the four constrains and you have to change the heights of both the labels priority to 750 & set the hugging and resistance priority to 1000. Then you have to change the number of lines to 0 in storyboard
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
Add this code into viewDidLoad() method:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44
In Swift 3, Use
func tableView(_ tableView: UITableView,estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return HEIGHT // Maximum cell height
}
In table view cell, Set dynamic height constraint to the text view (Greater than or equal) - Maximum text view height
I found the problem. After setting text to UILabel, it doesn't still set correct height frame to its content. In cellForRowAt, just add this:
//these make height frame both of labels became correct
cell.lbName.sizeToFit()
cell.lbAge.sizeToFit()
// update cell layout
cell.layoutIfNeeded()
Have a look at the UITableViewDelegate function tableView(_:heightForRowAt:). It’ll work in your case if you are able to calculate the height for a given index path.

How to adjust UITableView's height based on UILabel's height?

I'm having a UITableView. In that I have a single label. I am doing following for label:
Set number of lines to zero
Set top space to container margin
Set bottom space to container margin
My contents for UILabel may vary, some time it may have 3 lines, some times 10 lines etc. My problems is it displays only 2 rows. How to adjust the height of cell so that UILabel will show the whole contents.
Thanks in advance.
You should set numberoflines to 0. Provide either leading & trailing space constraints for your label or fixed width constraint along with top & bottom space constraints. Then use tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
set your label's constraint with cell like top bottom leading and trailing, set number of line for labels to zero and line break mode to word wrap. now place this two tableView methods to calculate cell height dynamically.
For Swift 3.0
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
for Objective C
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
so it will take cell size dynamically as you want.
hope this will help you.

Dynamic height of UITableview Issue

I have set
self.tableView.estimatedRowHeight = 88.0
self.tableView.rowHeight = UITableViewAutomaticDimension
Also I have done numberOfLines of UILabel to zero.
I haven't given any fixed height/width constraint in IB.
Still my UITableviewCell height is not expanded accordingly.
Please suggest me what to do.
Try This:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
EDIT
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Define above Both Methods.It Solve The Problem.
Check whether you are explicitly defining tableView:heightForRowAtIndexPath method.
If so remove that method or if you want to use it because of several cells existing there , you can return UITableViewAutomaticDimension for that particular cell height.
You should check that the first view from top, has a constraint to the top of the cell, and the bottom one, to the bottom of the cell. That way, the cell will know how big is its content.
And check your log for constraints conflicts.
You can use this for reference:
https://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

Resources