【问题标题】:Calculate UITableViewCell height with UIWebview in iOS在 iOS 中使用 UIWebview 计算 UITableViewCell 高度
【发布时间】:2016-05-19 12:40:51
【问题描述】:

我正在创建一个基于表格的应用程序,其中需要为每个表格单元格显示一个 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;
}

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

【问题讨论】:

    标签: ios objective-c uitableview ios7 uiwebview


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      预先将你的 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;
      

      【讨论】:

      • @Alexander感谢您分享您的答案,我试过了,但我的应用程序因错误“由于未捕获的异常 'NSRangeException' 而终止应用程序而崩溃,原因:'*** -[__NSArrayM objectAtIndex:]: index 21 超出范围为空数组'"。
      • 这个 if (height != [[arrayRowHeights objectAtIndex: aWebView.tag] floatValue]) 条件不满足,所以不要进去。 :(
      • 只需使用一些初始值(例如 1)初始化您的 arrayRowHeights。只需在viewDidLoad 方法中使用它:NSMutableArray* arrayRowHeights = [[NSMutableArray alloc] initWithCapacity:arrayEventInfo.count]; for (id _ in arrayEventInfo) { [arrayRowHeights addObject:@(1)]; }
      猜你喜欢
      • 2015-11-15
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多