简体   繁体   English

在iOS中使用UIWebview计算UITableViewCell高度

[英]Calculate UITableViewCell height with UIWebview in iOS

I am creating a table based application where need to display one UIWebView for each table cell. 我正在创建一个基于表的应用程序,其中需要为每个表单元格显示一个UIWebView。 I have tried by keeping a NSMutableArray of height for each cell in my table, but it's not working. 我已经尝试过为表格中的每个单元格保留一个高度为NSMutableArray的数组,但是它不起作用。

here is my code : 这是我的代码:

- (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;
}

My problem is I am not able to set the the height of the cell properly, can anyone please figure out where is my mistake on this given code. 我的问题是我无法正确设置单元格的高度,任何人都可以找出在此给定代码上我的错误在哪里。 Thanks! 谢谢!

Your webViewDidFinishLoad for each cell will get called only after the tablecell is loaded i,e; 您的每个单元格的webViewDidFinishLoad仅在加载表单元格后才会调用,即; So the sequence calls will be heightForRowAtIndexPath -> cellForRowAtIndexPath -> webViewDidFinishLoad . 因此,序列调用将为heightForRowAtIndexPath-> cellForRowAtIndexPath-> webViewDidFinishLoad。 So you will never be able to set the height before cell loads. 因此,您永远无法在单元加载之前设置高度。 After webView is loaded try calling 加载webView后,尝试调用

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

which will call heightForRowAtIndexPath again and try to set it again. 它将再次调用heightForRowAtIndexPath并尝试再次设置它。 But the height will increase at runtime .You should customize that event 但是高度会在运行时增加。您应该自定义该事件

Initialize your arrayRowHeights as NSMutableArray beforehand(in viewDidLoad method for instance) with the same capacity as arrayEventInfo.Change your webView delegate method to this one 预先将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];
    }
}

In method: 在方法中:

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

Change line 换线

webViewHTML.tag = arrayEventInfo.count;

with line 带线

webViewHTML.tag = indexPath.section;

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

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