【问题标题】:How to fix the height of tableviewcell based on webview height? (i am loading webview in each tableviewcell)如何根据 webview 高度修复 tableviewcell 的高度? (我在每个 tableviewcell 中加载 webview)
【发布时间】:2013-03-22 14:55:54
【问题描述】:

//I am loading **HTML file** in **UIWebview** which is located on **tableviewcell**.

我想根据 HTML 文件高度来固定单元格的高度。

注意:每个单元格加载的 HTML 文件会有所不同。(每个 HTML 文件的高度不是恒定的)

【问题讨论】:

标签: iphone objective-c uitableview


【解决方案1】:

要获取UIWebView 对象的高度,首先需要加载它们。然后在UIWebView的委托方法中,您可以根据下面给出的html内容获取高度。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"%f",myWebView.scrollView.contentSize.height);
}

你也可以像这样通过JS插入得到UIWebView的高度

[myWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];

webViewDidFinishLoad 方法中,您必须根据 webview 对象标签存储高度

然后加载您的表格并在下面的方法中相应地给出高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

【讨论】:

  • 对 swift 有什么帮助吗?而且我不清楚“在 webViewDidFinishLoad 方法中,您必须根据 webview 对象标签存储高度。”
【解决方案2】:

如果我理解正确的话..

在此处为 cell.textLabel 设置 cell.textLabel.lineBreakMode 和行数。 (0 - 无穷大)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];

       cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
       cell.textLabel.numberOfLines = 0;
   }

   cell.textLabel.text = [news objectAtIndex:indexPath.row];

   return cell;

}

这里需要计算单元格的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    NSString *cellText = [news objectAtIndex:indexPath.row];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
    CGSize constraintSize = CGSizeMake(320.0f, MAXFLOAT);

    CGSize labelSize = [cellText sizeWithFont:cellFont 
                            constrainedToSize:constraintSize 
                                lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 20.0f;
}

【讨论】:

    【解决方案3】:

    实现UITableViewDelegate方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *text = [self getItemForKey:kSummary];
        CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
        //You will need to define kDefaultCellFont
        CGSize labelSize = [text sizeWithFont:kDefaultCellFont 
                        constrainedToSize:constraintSize 
                            lineBreakMode:UILineBreakModeWordWrap];
        return labelSize.height + ANY_OTHER_HEIGHT;
    }
    

    如果您想了解更多信息,请查看Link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多