2015-03-03 2 views
0

Я работаю над проектом, где у меня есть UITableViewCell, содержащий UIImageView, UILable и UIWebView. Мне нужно открыть webview (с добавленной строкой HTML в нем) на didselectrowatIndexpathmethod и высоту ячейки следует увеличить размер содержимого UIWebView. Я пробовал следующий код. Но это не работает в ожидании. Прошу совета.Загрузите UIWebView в UITableViewCell и сделайте высоту UITabViewCell в соответствии с содержанием 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; 
} 

ответ

0

Вам нужно будет рассчитать (и, возможно, монитор) размер вашего контента в UIWebView. Один путь через UIWebView «s UIScrollView:

CGFloat webViewContentHeight = myWebView.scrollView.contentSize.height; 

другой является:

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

При извлечении, что высота важно, вы хотите его, как только содержание UIWebView закончил загрузку (раз ему вы можете аннулировать/обновить ограничения вашего UITableViewCell, например).