在UITableViewCell中加载UIWebView并使UITableViewCell';s高度根据UIWebview的内容

在UITableViewCell中加载UIWebView并使UITableViewCell';s高度根据UIWebview的内容,uitableview,uiwebview,Uitableview,Uiwebview,我正在从事一个项目,其中有一个包含UIImageView、UILable和UIWebView的UITableViewCell。 我需要在DidSelectRowatineXpathMethod上打开一个webview(其中添加了HTML字符串),单元格高度应增加到UIWebView的内容大小。我尝试了以下代码。但这并没有达到预期效果。好心的建议 #pragma mark - Table view data source -(NSInteger) tableView:(UITableView *)

我正在从事一个项目,其中有一个包含UIImageView、UILable和UIWebView的UITableViewCell。 我需要在DidSelectRowatineXpathMethod上打开一个webview(其中添加了HTML字符串),单元格高度应增加到UIWebView的内容大小。我尝试了以下代码。但这并没有达到预期效果。好心的建议

#pragma mark - Table view data source
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [faqContentArray count];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
     FAQCell *cell = (FAQCell*)[tableView cellForRowAtIndexPath:indexPath];
    [cell.faqContentWebView loadHTMLString:[[faqContentArray objectAtIndex:indexPath.row] objectForKey:@"content"] baseURL:nil];
    NSString *wrappedText = [[faqContentArray objectAtIndex:indexPath.row] objectForKey:@"content"];
    cell.faqContentWebView.frame=CGRectMake(8.0f, 43.0f, 304.0f, [self minHeightForText:wrappedText]);


    [self rotateIconToExpanded:cell.faqImage];
     [self.faqTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
//-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
//     FAQCell *cell = (FAQCell*)[tableView cellForRowAtIndexPath:indexPath];
//    
//    [cell.faqContentWebView loadHTMLString:@"" baseURL:nil];
//    cell.faqContentWebView.frame=CGRectMake(8.0f, 43.0f, 304.0f, 5);
//    [self rotateIconToCollapsed:cell.faqImage];
//     [self.faqTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FAQCell *cell = (FAQCell*)[tableView dequeueReusableCellWithIdentifier:@"MyCell"];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"FAQCell" bundle:nil] forCellReuseIdentifier:@"MyCell"];
        cell = (FAQCell*)[tableView dequeueReusableCellWithIdentifier:@"MyCell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
     }



    cell.faqContentWebView.delegate=self;
    cell.faqContentWebView.layer.cornerRadius = 0;
    cell.faqContentWebView.userInteractionEnabled = NO;
    cell.faqContentWebView.multipleTouchEnabled = YES;
    cell.faqContentWebView.clipsToBounds = YES;

    cell.faqContentWebView.scalesPageToFit = NO;
    cell.faqContentWebView.scrollView.scrollEnabled = NO;
    cell.faqContentWebView.scrollView.bounces = NO;
    cell.faqContentWebView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    cell.faqTitleLbl.text=[[faqContentArray objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.faqImage.image=[UIImage imageNamed:@"downarow.png"];

    return cell;
}
- (void)rotateIconToExpanded:(UIImageView *)iconImage {
    [UIView beginAnimations:@"rotateDisclosure" context:nil];
    [UIView setAnimationDuration:0.2];
    iconImage.transform = CGAffineTransformMakeRotation(M_PI * 2.5);
    [UIView commitAnimations];
}

- (void)rotateIconToCollapsed:(UIImageView *)iconImage {
    [UIView beginAnimations:@"rotateDisclosure" context:nil];
    [UIView setAnimationDuration:0.2];
    iconImage.transform = CGAffineTransformMakeRotation(M_PI * 2);
    [UIView commitAnimations];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    id celll = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    FAQCell *cell=(FAQCell *)celll;
    [cell setNeedsLayout];
    [cell layoutIfNeeded];
    NSLog(@"height------>%f",[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);
    return [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
}

您需要在
UIWebView
中计算(并可能监控)内容的大小。一种方法是通过
UIWebView
UIScrollView

CGFloat webViewContentHeight = myWebView.scrollView.contentSize.height;
另一个是:

CGFloat webViewContentHeight = [myWebView stringForEvaluatingJavaScript:@"document.body.offsetHeight"].floatValue;
当您检索到该高度时,当
UIWebView
的内容完成加载后,您将需要该高度(例如,一旦加载完成,您可以使
UITableViewCell
的约束失效/更新)