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

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..

Related

UITableview Height is not adjusting using UITableViewAutomaticDimension

I have tableview inside tableview cell , I am using UITableViewAutomaticDimension in heightForRowAt, tableview height is not adjusting automatically. I have to calculate cell height manually.
Any idea ?
You are missing either bottom or top constraint.
Consider you have to add label in table cell, then you have give both top and bottom constraint to label (make line number 0).
It will automatically increase the size of table cell according to label text.
Don't forgot to set estimatedRowHeight.
To get automatic cell height work correctly you must hook all your constraints properly from top to bottom in the cell Xib , or if you're creating constraints in code , that's how auto-layout can calculate the height automatically
self.tableView.estimatedRowHeight = 50.0
self.tableView.rowHeight = UITableViewAutomaticDimension
Set all constraint like bottom, top, left, right.

UITableView: Changing one cell's height constraint after table is constructed?

I might be wanting a strange thing, but the task is to stretch last cell in UITableView to fill the rest space. So in case table will only have few cells, then the last one will match it's height to the rest until screen bottom.
To achieve this, I included height constraint in the last cell's xib.
I can calculate proper height and set the constant, but it does not matter what I do afterwards - setNeedsDisplay(), setNeedsLayout(), reloadData(), reloadRowsAtIndexPaths() etc. - the cell height is not changed.
Only when I call the update code second time, layout is adjusted.
Any hints how to force update the cell immediately?
You need to have the following implemented (viewDidLoad is fine).
var estimatedRowHeight = 80 (for example)
// Set automatic dimension on row height.
tableView.rowHeight = UITableViewAutomaticDimension
// Set estimated row height.
tableView.estimatedRowHeight = estimatedRowHeight
Remove the height constraint and then call your resize with either the reloadData, or reloadRowsAtIndexPaths().
Did you try the following method? Where you check for the last indexPath.row and put a different height that you calculate.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

Self-Sizing TableView Cells based on two subview's height

TableView Description
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
Cell Description
I have a cell with 2 Labels(Title, Description) inside a ContainerView and 1 ImageView as below. Cell height will vary based on Description Label’s content.
Contraints of all views
There are two cases that I should handle
ContainerView.height greater than (ImageView.height + ImageView.Top + ImageView.Bottom). Here cell's height will be based on ContainerView.height
ContainerView height less than (ImageView.height + ImageView.Top + ImageView.Bottom). Here I expect Cell should consider (ImageView.height + ImageView.Top + ImageView.Bottom) as the height and make ContainerView vertically centre to Cell.
Expected Result in both the cases
Problem
If I set constraints for 1st case then 2nd case is not working and vice versa (I’m aware that by removing ContrainerView.Top, Bottom and making it Vertically Centre to SuperView case 2 result can be achieved)
Is there a way to achieve expected result in both the cases by using same set of IB constraints and UITableViewAutomaticDimension?
Give fixed height and width to the image view . Otherwise let the tableViewCell know the top and bottom of the imageview. So that it can calculate the correct cell height
First make sure that you are using self-sizing cells:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
Make Top and Bottom constraints for both image view and the container view to the edges of the cell and make them >=.
Alternatively, you could try Horizontal Stack View and make rigid (highest priorities) constraints to each edge of the cell.
Use these delegates,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
Edit
Try this one.
Your view hierarchy should be like this
ImageView Constraints
Add a view and put both labels into it.
Label Container contraints
Label Title constraint
Label Description constraint
Edit Output

Dynamic UITableViewCell content does not expand cell

I have a storyboard with several different types of prototype cells with UILabels containing dynamic data. In my storyboard, the cell looks like this:
The UILabel's Lines property is set to zero to allow multiple lines of text. It is pinned to the top, left, right of the content view and to the nearest neighbor on the bottom (the blue line). The blue line is pinned to the left and right of the content view and to the UILabel at the top, and the UITextView at the bottom.The UITextView is pinned to the content view on the bottom, left and right, and to the blue line at the top.
When I run the app I get the following:
So the UILabel is forcing everything else down, as it should be, but the cell's height does not change as I want it to and thus the text view is being clipped off by the cell's fixed height. It was my assumption that if everything were pinned at the top and bottom, then the content view would be forced to expand. What am I missing here? Thanks!
It seems you have set your constraints correctly, just make sure that delegates are as below:
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
(If your constraints are set correctly from top to bottom)And that's it, you dont have to do anything else, auto layout will do its work smartly.
In addition to Auto-Layout constraint
try cell.contentView.LayoutIfNeeded statement in cellForRowIndexPath just before returning cell and HeightForRowAtIndexPath method
If you're sure all your vertical constraints are set and correct top to bottom, try setting tableView.rowHeight = UITableViewAutomaticDimension explicitly.

Problems With Dynamically Resizing UITableViewCells

I'm attempting to dynamically resize a custom UITableViewCell to fit an attributed string's content. This is the result:
When I initialize my tableView I do the following:
self.chatsTableView.estimatedRowHeight = 72.0;
self.chatsTableView.rowHeight = UITableViewAutomaticDimension;
I also implemented the following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
As you can see it is resizing my height (the smaller allowed is 72). However, for some reason it's creating a weird effect. I'm using storyboard, and auto layout.
Move it to the - (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath method.
You have to set estimatedRowHeight to UITableViewAutomaticDimension as well.
It should work only with the two properties you set , the row height and the estimated row height.
first try to remove the delegate method.
If the table "Jumps" a little when you scroll (because of a bug) then also implement the two delegate methods, heightForRow and estimatedHeightForRow and return the same values as you set in the properties , estimatedRowHeight = 72 and rowHeight = UITableViewAutomaticDimension.
If it's still doesn't work then I would double check the constraints.
For iOS 8+
Set up cell constraints. Make sure that ALL subviews that affects cell height are vertical connected. In this case: top edge of top label is pinned to superview's top AND top labels bottom edge is pinned to bottom's label top edge AND bottom's label bottom edge is pinned to superview's bottom. Vertical constraints chain have to be closed. In your case it looks like a bottom edge of bottom label is not pinned to contentViews bottom edge.
When dequeuing cell for reuse always use
dequeueReusableCellWithIdentifier:forIndexPath: not a shorter version without indexpath specified.
Set self.tableView.rowHeight = UITableViewAutomaticDimension;
Set self.tableView.estimatedRowHeight = 50.0;. This step is optional but helps table view proper manage things like scroll indicators etc.
Will work :)

Resources