根据iOS中的UIWebView内容计算表格单元格高度?

根据iOS中的UIWebView内容计算表格单元格高度?,ios,uitableview,ios7,uiwebview,Ios,Uitableview,Ios7,Uiwebview,我正在创建基于表视图的应用程序,当用户点击任何表行时,我设置了一个UIWebView,以显示为具有不同html内容的可扩展UITableView。它工作得很好,但我的问题是如何根据html内容设置单元格高度,在StackOverFlow中有很多类似的问题,但我的情况有所不同。我知道如何使用UIWebView委托方法计算内容高度,但如何在这里应用 这是我的代码: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

我正在创建基于表视图的应用程序,当用户点击任何表行时,我设置了一个UIWebView,以显示为具有不同html内容的可扩展UITableView。它工作得很好,但我的问题是如何根据html内容设置单元格高度,在StackOverFlow中有很多类似的问题,但我的情况有所不同。我知道如何使用UIWebView委托方法计算内容高度,但如何在这里应用

这是我的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.arrayHtmlDescription count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIWebView *webView;
    static NSString* cellIdentifier = @"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        webView = [[UIWebView alloc] init];
        webView.frame = CGRectMake(0, 0, cell.bounds.size.width, 500);
    }

    webView.userInteractionEnabled = NO;
    [webView sizeToFit];
    webView.delegate = self;
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    [webView loadHTMLString: [[self.arrayHtmlDescription objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] baseURL: nil];

    [cell.contentView addSubview:webView];

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500; // need to set this height dynamically
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    webView.scrollView.scrollEnabled = NO;
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    NSLog(@"webview height is : %f", fittingSize.height);
}
计算UIWebView高度的代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.arrayHtmlDescription count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIWebView *webView;
    static NSString* cellIdentifier = @"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        webView = [[UIWebView alloc] init];
        webView.frame = CGRectMake(0, 0, cell.bounds.size.width, 500);
    }

    webView.userInteractionEnabled = NO;
    [webView sizeToFit];
    webView.delegate = self;
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    [webView loadHTMLString: [[self.arrayHtmlDescription objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] baseURL: nil];

    [cell.contentView addSubview:webView];

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 500; // need to set this height dynamically
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    webView.scrollView.scrollEnabled = NO;
    CGRect frame = webView.frame;
    frame.size.height = 1;
    webView.frame = frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    NSLog(@"webview height is : %f", fittingSize.height);
}

请建议我如何将fittingSize传递给heightForRowAtIndexPath表视图方法。谢谢

为表中的每个单元格保留NSMutableArray高度,并在委托方法中使用该值:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.height array objectAtIndex:indexPath.row]; 
}
计算webview的高度时,请更新此数组并调用reloadRowsAtIndexPaths方法或重新加载整个tableview

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // Caluclate the height and update the array of heights.
[self.tableview reloadData];


}
编辑: 要计算nsindepath,可以使用UIWebView的标记属性,如下所示:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Your code to create webview

     webview.tag = indexPath.row;
    [cell.contentView addSubview:webView];

    return cell;
}
现在,在计算高度后,可以使用以下方法创建索引:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
        // Caluclate the height and update the array of heights.
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:webView.tag inSection:0];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]] withRowAnimation:UITableViewRowAnimationFade];



    }

计算webview高度后,重新加载单元格,以便调用heightForRowAtIndexPath:delegate方法。使用这里的高度。好的,谢谢@jithinroy,我已经更新了代码,请检查。你能告诉我需要在哪里重新加载表格单元格@jithinroy吗?谢谢我的意思是,在webViewDidFinishLoad的末尾:调用以下命令:[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]with RowAnimation:UITableViewRowAnimationFade];您需要为webview计算单元格的NSIndexPath。但我不明白如何为webview计算单元格的NSIndexPath?我将1定义为numberOfRowsInSection。实际上,我只为numberOfRowsInSection中的每个节定义了1行。所以,webview.tag每次都是0,第二个问题是我是否会使用[self.tableview reloadData];在webViewDidFinishLoad方法中,则此方法将调用无限次,bcos我已在CellForRowatingIndexPath中创建UIWebView。使用webView.tag=indexPath.section,在webViewDidFinishLoad中,indexPath将是:NSIndexPath*IndexXPath=[NSIndexPath indexPathForRow:0第二节:webView.tag];请尝试重新加载RowsatindeXPath,而不是重新加载数据