繁体   English   中英

在iOS中使用UIWebview计算UITableViewCell高度

[英]Calculate UITableViewCell height with UIWebview in iOS

我正在创建一个基于表的应用程序,其中需要为每个表单元格显示一个UIWebView。 我已经尝试过为表格中的每个单元格保留一个高度为NSMutableArray的数组,但是它不起作用。

这是我的代码:

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
    aWebView.scrollView.scrollEnabled = NO;
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

    if (arrayRowHeights.count == 0 || arrayRowHeights == nil) {

        arrayRowHeights = [[NSMutableArray alloc] init];
        [arrayRowHeights addObject:[NSString stringWithFormat:@"%f", fittingSize.height]];
    }
    else{
        [arrayRowHeights addObject:[NSString stringWithFormat:@"%f", fittingSize.height]];

        if (arrayRowHeights.count == aWebView.tag) {
            [tblEventInfo reloadData];
        }
    }
}

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [[arrayRowHeights objectAtIndex:indexPath.section] floatValue];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        EventInfoDetail *event = [[EventInfoDetail alloc] init];
        event = [arrayEventInfo objectAtIndex:indexPath.section];

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        webViewHTML = [[UIWebView alloc]init];
        webViewHTML.delegate = self;
        webViewHTML.tag = arrayEventInfo.count;
        webViewHTML.backgroundColor = [UIColor clearColor];
        webViewHTML.opaque = NO;
        webViewHTML.userInteractionEnabled = NO;

        if (event.d_description == (NSString *)[NSNull null]){
            [webViewHTML loadHTMLString:@"" baseURL: nil];
        }
        else{
            [webViewHTML loadHTMLString:event.d_description baseURL: nil];
        }

        webViewHTML.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);

        [cell.contentView addSubview:webViewHTML];
    }

    return cell;
}

我的问题是我无法正确设置单元格的高度,任何人都可以找出在此给定代码上我的错误在哪里。 谢谢!

您的每个单元格的webViewDidFinishLoad仅在加载表单元格后才会调用,即; 因此,序列调用将为heightForRowAtIndexPath-> cellForRowAtIndexPath-> webViewDidFinishLoad。 因此,您永远无法在单元加载之前设置高度。 加载webView后,尝试调用

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];}

它将再次调用heightForRowAtIndexPath并尝试再次设置它。 但是高度会在运行时增加。您应该自定义该事件

预先将arrayRowHeights初始化为NSMutableArray(例如,在viewDidLoad方法中),其容量与arrayEventInfo相同。将webView委托方法更改为此

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
    CGFloat height = aWebView.scrollView.contentSize.height;
    if (height != [[arrayRowHeights objectAtIndex: aWebView.tag] floatValue]) {
       [arrayRowHeights insertObject:[NSNumber numberWithFloat: height] atIndex: aWebView.tag];
       [tblEventInfo reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:aWebView.tag]] withRowAnimation:UITableViewRowAnimationNone];
    }
}

在方法中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

换线

webViewHTML.tag = arrayEventInfo.count;

带线

webViewHTML.tag = indexPath.section;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM