0

Table view cell

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.

Arun balaji
  • 101
  • 3
  • 12
  • you need to set the cell height or you can do it dynamically – Rashed Mar 31 '18 at 06:01
  • how to set it dynamically ? – Arun balaji Mar 31 '18 at 06:04
  • should I calculate the cell height based on the text size? – Arun balaji Mar 31 '18 at 06:05
  • Try this excellent explanation - https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – Rashed Mar 31 '18 at 06:23
  • Possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) – Shivam Tripathi Mar 31 '18 at 06:36
  • @Arunbalaji Well by now you must have got the answer to your question but why are you using UIView at the place of UINavigationBar when you can do all this using UINavigationBar. – Anirudha Mahale Apr 25 '18 at 14:51
  • @AnirudhaMahale I am not familiar to iOS coding I did not know about UINavigation bar, Now I switched to it. Thank you – Arun balaji May 03 '18 at 06:10

5 Answers5

3

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;
Si Nguyen
  • 161
  • 8
2

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.

enter image description here

enter image description here

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.

kalpesh
  • 1,285
  • 1
  • 17
  • 30
1

I think your cell is not gettings its height because of textview scrolling maybe that will help:-

  1. Use UITableViewAutomaticDimension and for that your tableview cell content should justify both top-bottom leading and trailing constraints to calculate cell height.
  2. Set scrollEnabled to "false" for your textview.

With everything on the place your cell will get its height for sure.

tryKuldeepTanwar
  • 3,490
  • 2
  • 19
  • 49
1

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

Krishna Maru
  • 164
  • 13
1

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

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32