0

Hi in my application i have a requirement to display prodcuts with description in tablview. For achieving this i added two satckviews on tablview cell content view. Both stackviews holds labels and one label have a prodcut name and another one have description. At app launch the app cell height is 100 by default, but when ever i select cell i want to show complete description in cell along with product name i mean need to change cell height.Can anyone please let help me how to achieve this.How to change cell height dynamically. Please help me.

I tried below approach it shows more description in stack view label but cell height is not getting change. So unable to see total description.

cell.productDescription.numberOfLines=50;
cell.productDescription.lineBreakMode=NSLineBreakByCharWrapping;
Naresh G
  • 117
  • 1
  • 10
  • check this answer http://stackoverflow.com/questions/41439786/aligning-multiple-runtime-generated-uilabels-in-a-uitableview/41440763#41440763 if you need to do it programmatically or else go with self sizing tableView cells – Vinupriya Arivazhagan Feb 13 '17 at 11:04
  • @Naresh G you want it on cell tap or when your table view loads it automatically adjust its cell height? – Tushar Sharma Feb 13 '17 at 11:56
  • You can change the numberOfLines to 0, to remove any maximum limit, and use as many lines as needed. – Saurabh Yadav Feb 13 '17 at 11:59

2 Answers2

1

Best way to use Constraint for your application. So you don't have to manage heighforCell. If you are going to coding part then you have to find out height of text and then you need to set in heightForRowAtIndexPath method.

Here is the code to get height from string:

-(float )getHeightForText : (NSString *)strText
{
    UILabel *lbl = [[UILabel alloc] init];
    CGRect rect;
    rect.origin = CGPointZero;
    float width = YOUR_LABEL_WIDHT
    rect.size = CGSizeMake(width, 3000);
    lbl.font = MESSAGE_TEXT_FONT;
    lbl.frame = rect;
    lbl.text = strText;
    lbl.numberOfLines = 1000;
    [lbl sizeToFit];

    float height = lbl.frame.size.height;
    return height;
}

Write below code in your heightForRowAtIndexPath method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self getHeightForText:YOUR_TEXT];
}
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56
0

Use UITableViewDelegate Method

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44