how to set tablecell row height automatically based on inner view outlet? - ios

I want to set tableview cell height automatically to show the label completely without happening like that?
Any help?

There are 2 things that you need to do:
1st thing: Set AutoLayout for it.
2nd thing: implement these code:
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100
You can see more from my project: https://github.com/khuong291/Yelp

Related

Custom TableView does not set a custom height for custom TableViewCell

I am trying to make a custom TableViewCell for a tableview, and the size is 414x350. The problem I am having is when table view get loaded all the sizes are squeezed and not right. I have tried all of the followings:
Assigning row height from code
tableView.estimatedRowHeight = 350
tableView.rowHeight = UITableView.automaticDimension
Assigning row height from storyboard in tableview window
Assigning row height from storyboard in tableviewcell window
The result is still the same. Any help is appreciated!
For any cell's automatic row dimension to be calculated properly, in your Storyboard you must have a direct line of vertical constraints between every item that goes all the way from the top to the bottom. If you set the constraints yourself, same thing applies.
Don't assign any row heights, except the estimated.

Why is tableView cell not dynamic?

Update:
I have found that the problem occurs because of the date labels top constraint when removed the wrapped text becomes visible. The issue is then the date label is under the other text. Could the real problem be that the cell is not dynamically changing based on the constraints?
I am trying to make text in a UILabel to wrap (at the same time I am trying to make the cell change based on the text)
In the .xib I setup the cell, doing this:
self.notifInfoLabel.numberOfLines = 0
self.notifInfoLabel.lineBreakMode = .byWordWrapping
sizeToFit is called right before returning the cell in the delegate method.
cell.notifInfoLabel.sizeToFit()//EDIT: turns out this is not needed
return cell
}
The configure method is called on viewDidLoad and right before .reloadData()
func configureTableView() {
self.tableView.estimatedRowHeight = 82
self.tableView.rowHeight = UITableView.automaticDimension
}
Currently, the text in the label seems to wrap underneath the date label...
How can I make it work as intended?
You have confusing constraints for Auto layout engine.
You want some space from bottom of image view if text is small , And
you want text to dynamic in that case label should grow.
Solution is very simple.
Bottom constraint of Imageview change = to >= with 750 priority
Bottom constraint of Date change = to >=
Hope it is helpful

UITabIeView CustomCell is not working properly before scrolling... in swift

I am trying to design a comments Controller but unfortunately i am doing something wrong . I used uiTableview for this purpose.
On run time my customcell with more comment text is hiding behind the customcell with less amount of data.
This is the layout structure
As shown above customcell has a uiimageview and a stackview. Stackview has to uilabels. I also setted stackview distribution to proportionally.
Although when i scroll everything becomes fine.
I also used this in on ViewDidLoad
tableView.rowHeight = UITableView.automaticDimension
Looks like a constrains issue still few steps you can do
Set constrains properly, Number of lines = 0 to label
Make sure of below code you use in viewdidload
tableView.estimatedRowHeight = YourTableViewHeight
tableView.rowHeight = UITableViewAutomaticDimension

UI Tableview cell not expanding when text is added at the runtime

I am using table view for my chat application, in which the chat responses are added either one of two textviews as shown in the image. The thing is whenever I add a large text, the cell height does not expand to show the full text. It only shows the last line of that text. I am using objective-c. Is there a way to solve this ?. I do not want to completely create a new view like the questions asked before, I want changes to this specific view.
First, you need to use Label and set top and bottom constraint between label and contentView cell.
In order to allow the self-sizing cell mechanism to work, you must set the rowHeight property on the table view to the constant UITableViewAutomaticDimension. Then, you simply need to enable row height estimation by setting the table view's estimatedRowHeight property to a nonzero value, for example:
_tableView.estimatedRowHeight = 44.0; // set to whatever your "average" cell height is
_tableView.rowHeight = UITableViewAutomaticDimension;
You need to defined self-sizing cell like this.
tableCountries.estimatedRowHeight = 100.0;
tableCountries.rowHeight = UITableViewAutomaticDimension;
Make sure you have a proper constraint. You can have a constraint like this.
In the below label you need to set your textView top position. That's why when the below label expands it automatically expand label and text view will push to bottom.
I think your cell is not gettings its height because of textview scrolling maybe that will help:-
Use UITableViewAutomaticDimension and for that your tableview cell content should justify both top-bottom leading and trailing constraints to calculate cell height.
Set scrollEnabled to "false" for your textview.
With everything on the place your cell will get its height for sure.
you can do it in many ways :
1) You can set dynamic height of cell using UITableViewAutomaticDimension.
You need to set proper constraints in storyboard like setting top and bottom.
in ViewDidLoad add below :
tblView.rowHeight = UITableViewAutomaticDimension
tblView.estimatedRowHeight = 100
and in tableView cell's height method :
return UITableViewAutomaticDimension
2) Find text's height dynamically using below function :
let width = 100.0 \\your text label's width you gave
var height = chatMsg.text?.height(withConstrainedWidth: width, font: chatMsg.font!)
using above code you can find the height of your text. and you can use it dynamically
3) there are many code available to use for chat application in which you just need to pass text/image, and it will handle everything else.
Link : https://github.com/kerry/iMessageBubble
Add these two delegate methods:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
Create Custom UITableViewCell with Three UILabel like lbl1,lbl2,lbl3.
Add the AutoLayout Constraints to lbl1 left, right, top and bottom. With fixed height (30).
Then set same constraints to remaining two Labels.
Here is main things:
1.select lbl1 heigh and double click on height constraint and set >=30
Do same things with lbl2 and lbl3.
Final step is:
Select lbl2 top constraints, change the constraints property like <=8.
Do same things with lbl3, that's it we are done all.
thanks..

Auto layout cell based on subview or custom cell height

In the image. Comments has a textview that has text overflown to below 3 cells . Ideally that should not happen.
I have a tableview with custom cells as xib files and some custom cells are subviews. Please let me know how to add auto layout for this subviews or xib's.
I read many posts they suggested.below code. But it doesn't work.
In ViewDidLoad.
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 50;
Please suggest a solution. Thank you.
It will works, but you need to update the height everytime a new character is input, so in your cellForRow, add textview.delegate = self, and in the textViewDidChange, do tableView.beginUpdate() and tableView.endUpdate(), the cell height will update correctly

Resources