【问题标题】:UIWebView in UITableViewCellUITableViewCell 中的 UIWebView
【发布时间】:2013-11-07 19:27:09
【问题描述】:

伙计们,

我想制作 UITableView,其中一些单元格是 web 视图。

所以,我已经为他们加载了 html。

我的代码:

 NSString *md5HTML = [adm_post_dict[@"html"] MD5];

        SKWebViewCell *cell = [tableView dequeueReusableCellWithIdentifier:md5HTML];

        if (!cell) {
            cell = [[SKWebViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:md5HTML];

            cell.owner = self;
            cell.height = [adm_post_dict[@"height"] floatValue];
            cell.html = adm_post_dict[@"html"];
        }

        return cell;

SKWebViewCell.m:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
        [webView setScalesPageToFit:NO];
        webView.scrollView.scrollEnabled = NO;
        [self.contentView addSubview:webView];

        self.contentView.backgroundColor = [UIColor redColor];
    }
    return self;
}
- (void) prepareForReuse {
    [webView stopLoading];
}
- (void) setHeight:(float)height {
    webView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, height);
}
- (void) setOwner:(NSObject<UIWebViewDelegate> *)owner {
    [webView setDelegate:owner];
}

- (void) setHtml:(NSString *)html {
    if (![_html isEqualToString:html]) {
        _html = html;

        [webView stopLoading];
        [webView loadHTMLString:[NSString stringWithFormat:@"<!DOCTYPE html><html><body>%@</body></html>",html] baseURL:[NSURL URLWithString:@"http://stylefie.com/"]];
    }
}


@end

问题:

  1. 它适用于 iOS 6,但速度很慢。
  2. 它在 iOS 7 上运行很快,但有时 Web 视图不显示任何内容并将其发送到控制台:

void SendDelegateMessage(NSInvocation *): 委托 (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) 等待 10 秒后未能返回。主运行循环模式: kCFRunLoopDefaultMode

您有什么想法,我该如何解决这个问题?有没有其他方法可以做同样的工作?

【问题讨论】:

    标签: ios iphone xcode uitableview uiwebview


    【解决方案1】:

    尝试将 baseURL:nil 放在

    [webView loadHTMLString:[NSString stringWithFormat:@"<!DOCTYPE html><html><body>%@</body></html>",html] baseURL:nil];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多