简体   繁体   中英

Calculate table cell height according to the UIWebView content in iOS?

I am creating table view based application, when user will tap on any table row then i have set a UIWebView for displaying as Expandable UITableView having different html contents. It's working fine but my problem is how to set cell height according to the html contents, there is lots of similar question in StackOverFlow but mine case is some what different. I know that how to calculate content height using UIWebView delegate methods, but how can i apply here?

here is my code :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.arrayHtmlDescription count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIWebView *webView;
    static NSString* cellIdentifier = @"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        webView = [[UIWebView alloc] init];
        webView.frame = CGRectMake(0, 0, cell.bounds.size.width, 500);
    }

    webView.userInteractionEnabled = NO;
    [webView sizeToFit];
    webView.delegate = self;
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    [webView loadHTMLString: [[self.arrayHtmlDescription objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] baseURL: nil];

    [cell.contentView addSubview:webView];

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500; // need to set this height dynamically
}

Code for calculating height of UIWebView :

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    webView.scrollView.scrollEnabled = NO;
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    NSLog(@"webview height is : %f", fittingSize.height);
}

Please suggest me how to pass fittingSize to heightForRowAtIndexPath table view method. Thanks!

Keep a NSMutableArray of height for each cell in your table and use that value in the delegate method :

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.height array objectAtIndex:indexPath.row]; 
}

When you calculate the height for the webview, update this array and call reloadRowsAtIndexPaths method or reload the entire tableview.

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Caluclate the height and update the array of heights.
[self.tableview reloadData];


}

EDIT: To caluclate NSIndexPath you can use tag property of UIWebView as shown below :

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Your code to create webview

     webview.tag = indexPath.row;
    [cell.contentView addSubview:webView];

    return cell;
}

Now after caluclating the height you can create the indexpath using:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
        // Caluclate the height and update the array of heights.
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:webView.tag inSection:0];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]] withRowAnimation:UITableViewRowAnimationFade];



    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM