2015-03-03 91 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的約束條件)。

相關問題