在UITableViewCell中加载UIWebView,并根据UIWebview的内容使UITabViewCell的高度



我正在做一个项目,其中有一个UITableViewCell,包含UIImageView、UILable和UIWebView。我需要在didselecrowatIndexpathmethod上打开一个网络视图(其中添加了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中内容的大小。一种方法是通过UIWebViewUIScrollView:

CGFloat webViewContentHeight = myWebView.scrollView.contentSize.height;

另一种是:

CGFloat webViewContentHeight = [myWebView stringForEvaluatingJavaScript:@"document.body.offsetHeight"].floatValue;

当您检索到该高度很重要时,一旦UIWebView的内容完成加载,您就会想要它(例如,一旦它完成加载,就可以使UITableViewCell的约束无效/更新)。

最新更新