3

// I am loading **HTML file** in **UIWebview** which is located on **tableviewcell**.

I want to fix height for cell based on HTML file height.

Note: loading HTML file will be different for every cell.(height is not constant for every HTML file)

Vishal
  • 8,246
  • 6
  • 37
  • 52
Lokesh Chowdary
  • 816
  • 5
  • 22
  • Please try this below link: http://stackoverflow.com/questions/13448426/change-uitableviews-height-to-fit-its-content-in-xcode/13448610#13448610 May it helps you. :) – Abha Mar 21 '13 at 12:13
  • For example [Try This Answer][1], hope it helps [1]: http://stackoverflow.com/a/4890119/1091539 – Mutawe Mar 21 '13 at 13:39

3 Answers3

4

To get the height of UIWebView object, first you need to load them. Then inside the delegate method of UIWebView you can get the height as per the html content like given below.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"%f",myWebView.scrollView.contentSize.height);
}

you can also get the height of UIWebView by JS insertion like this

[myWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];

In webViewDidFinishLoad method you have to store height based on webview object tag.

After that load your table and in below method give height accordingly.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Ankur
  • 5,086
  • 19
  • 37
  • 62
  • Any help in swift?And I don't clear that topic "In webViewDidFinishLoad method you have to store height based on webview object tag." – Thiha Aung May 17 '16 at 08:45
2

If I understood correctly..

Here you sets cell.textLabel.lineBreakMode and number of lines, for cell.textLabel. (0 - infinity)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];

       cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
       cell.textLabel.numberOfLines = 0;
   }

   cell.textLabel.text = [news objectAtIndex:indexPath.row];

   return cell;

}

Here you need to count the height of cell.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    NSString *cellText = [news objectAtIndex:indexPath.row];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
    CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);

    CGSize labelSize = [cellText sizeWithFont:cellFont 
                            constrainedToSize:constraintSize 
                                lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20.0f;
}
Arthur
  • 1,740
  • 3
  • 16
  • 36
1

Implement the UITableViewDelegate method - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = [self getItemForKey:kSummary];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    //You will need to define kDefaultCellFont
    CGSize labelSize = [text sizeWithFont:kDefaultCellFont 
                    constrainedToSize:constraintSize 
                        lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + ANY_OTHER_HEIGHT;
}

If you want more check the Link

moosa0709
  • 476
  • 3
  • 12