3

I am using UIWebView as Content for UITableViewCell. When I add UITableView, I am using some constant height for UITableViewCell. as no content is loaded in UIWebView. when Actual content is added in UIWebView, I am able to get actual height of UIWebView.by using [UIWebView sizeThatFits:CGSizeZero];

Now problem is UIWebView is with proper height but cell's height in smaller than UIWebView's new height. how I can update height for that cell?

Sagar...
  • 1,062
  • 3
  • 15
  • 35
  • it may help you or possible duplicate. http://stackoverflow.com/questions/8665812/iphone-how-to-increase-the-height-of-cell-dynamically-as-per-the-string – iPhone Jan 05 '12 at 12:08
  • I need to implement same way, but in above example its text and in my case I am downloading content from server and then adding in UIWebView. – Sagar... Jan 05 '12 at 12:24

4 Answers4

4

After the UIWebView has finished loading you should call

[tableView beginUpdates];
[tableView endUpdates];

This will call -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath again will resize your UITableViewCell. In that method you calculate the new height for the cell based on the size of your UIWebView.

Florian Mielke
  • 3,310
  • 27
  • 31
2

put this Method in your code it will work

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [[arrLocation objectAtIndex:indexPath.row] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 5;
}
beryllium
  • 29,669
  • 15
  • 106
  • 125
hirentank
  • 414
  • 3
  • 15
0

Inside this method you can change the row height dynamically

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Mudit Bajpai
  • 3,010
  • 19
  • 34
  • In - (void)webViewDidFinishLoad:(UIWebView *)aWebView { [iListTableView beginUpdates]; [iListTableView endUpdates]; } calling this but no success. – Sagar... Jan 05 '12 at 12:44
0

You can refer this sample for help:

http://github.com/penso/webview-in-tableview

Try this code:

- (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"Webview in UITableView";
        webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)]; // Don't use CGRectZero here, won't work
        webview.delegate = self;
        webview.hidden = YES;

        [webview loadRequest:[[NSURLRequest alloc] 
                              initWithURL: [NSURL URLWithString:@"http://www.google.com/"]]];

        [self.tableView setTableFooterView:webview]; // Leave this, else you'll have rows to fill the rest of the screen

    }

    - (void) webViewDidFinishLoad:(UIWebView *)webView {
        float newSize = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue];
        NSLog(@"Resizing webview from %.2f to %.2f", webView.frame.size.height, newSize);
        webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, newSize);
        webView.hidden = NO;

        // We need to reset this, else the new frame is not used.
        [self.tableView setTableFooterView:webview];
    }
nithin
  • 2,457
  • 1
  • 30
  • 50