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

时间:2015-03-03 10:45:30

标签: uitableview uiwebview

我正在开发一个项目,我有一个包含UIImageView,UILable和UIWebView的UITableViewCell。 我需要在didselectrowatIndexpath方法中打开一个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;
}

1 个答案:

答案 0 :(得分:0)

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

CGFloat webViewContentHeight = myWebView.scrollView.contentSize.height;

另一个是:

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

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