2015-03-03 77 views
0

我正在研究一个项目,其中我有一个包含UIImageView,UILable和UIWebView的UITableViewCell。 我需要在didselectrowatIndexpath方法中打开一个webview(带有添加的HTML字符串),并且单元格高度应该增加到UIWebView的内容大小。我试过下面的代码。但这不符合预期。友善的建议。在UITableViewCell中加载UIWebView,并根据UIWebview的内容制作UITabViewCell的高度

#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; 
} 

回答

0

您需要计算(并可能监测)UIWebView中的内容大小。一种方式是通过UIWebViewUIScrollView

CGFloat webViewContentHeight = myWebView.scrollView.contentSize.height; 

另一个是:

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

当您检索高度是很重要的,你会想它一旦UIWebView的内容已经完成加载(一旦它例如,您可以无效化/更新您的UITableViewCell的约束条件)。