【问题标题】:UITableViewCell error loading UIWebViewUITableViewCell 错误加载 UIWebView
【发布时间】:2012-08-03 00:29:49
【问题描述】:

如您所见,有一个错误。我在每个 UITableViewCell 上加载一个 UIWebView。问题是当它加载大量数据时 UITableViewCell 消失了,当我滚动文本覆盖本身时。

也许 UITableViewCell 有一个最大高度?

这是另一个错误?

我能做什么?

编辑:这是管理 API 响应并将每个“帖子”存储为 UIWebView 的 viewController 代码,它被加载到主视图和 UITableViewCell contentView 中。问题是:大型 webView 的问题在哪里?代码?最大高度?内存?

#import "TopicViewController.h"
#import "HTMLController.h"

NSString *_topicID;

@implementation TopicViewController

@synthesize heightForView = _heightForView;
@synthesize tableView = _tableView;
@synthesize htmlController = _htmlController;
@synthesize pageCounter = _pageCounter;

+ (void)setTopicID:(NSString *)topicID {
    _topicID = topicID;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)didReceiveMemoryWarning
{    
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.heightForView = [[NSMutableDictionary alloc] init];

    self.htmlController = [[HTMLController alloc] init];
    [self.htmlController requestTopicWithID:_topicID];

    NSInteger viewTag = 117;
    for (NSString *html in self.htmlController.postsContent) {
        NSString *formattedHTML = [NSString stringWithFormat:@"<div style='overflow: hidden; max-width: device-width;'>%@</div>", html];
        NSLog(@"%@", formattedHTML);
        UIWebView *content = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 1)];
        content.tag = viewTag;
        content.alpha = 0;
        content.opaque = NO;
        content.backgroundColor = [UIColor clearColor];
        content.scrollView.scrollEnabled = NO;
        content.scalesPageToFit = NO;
        content.delegate = self;
        [self.view addSubview:content];
        [content loadHTMLString:formattedHTML baseURL:[NSURL    URLWithString:@"http://www.elotrolado.net"]]; 
        NSLog(@"loading wv %i", viewTag);
        viewTag++;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -  TableViewDelegate & DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.heightForView count]>0 ? [self.heightForView count] : 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return nil;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self.heightForView count]>0 ? [[self.heightForView objectForKey:[NSString    stringWithFormat:@"%i", indexPath.section+117]] floatValue]+20.0 : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = [NSString stringWithFormat:@"%i", indexPath.section];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        UIView *content = [self.view viewWithTag:(indexPath.section+117)];
        content.alpha = 1;
        [cell.contentView addSubview:content];
    }

    return cell;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    CGRect frame = webView.frame;
    frame.size = [webView sizeThatFits:CGSizeZero];
    frame.size.width = 300;
    webView.frame = frame;

    [self.heightForView setObject:[NSString stringWithFormat:@"%f", frame.size.height] forKey:[NSString stringWithFormat:@"%i", webView.tag]];

    if ([self.heightForView count]==[self.htmlController.postsContent count]) {
        NSLog(@"All wv loaded");
        [self.tableView reloadData];
    } else NSLog(@"wv %i loaded", webView.tag);
} 

@end

此代码适用于大多数 webViews,但不适用于大型 webViews

【问题讨论】:

  • 能把相关代码也发一下吗?
  • 好吧,我不知道该发布什么,所以我将发布viewController的整个代码
  • 也许这无关紧要,但您不应为每个单元格使用不同的单元格标识符。您正在阻止单元格被重复使用,如果您有很多单元格,这可能会导致严重的内存问题。你可能想继承UITableViewCell 并添加一个UIWebView ,然后在那里更改大小。您还在viewDidLoad 加载所有网络视图吗?您可能想延迟加载它们..
  • 使用唯一标识符的目的是避免重新加载网页视图。单元格的数量是静态的,始终为 10。无论如何谢谢 =)

标签: ios xcode uitableview uiwebview


【解决方案1】:

我找到了答案here (tableView:heightForRowAtIndexPaths: 文档)

重要由于底层实现细节,您不应返回大于 2009 的值。

所以我的解决方案:

CGFloat height = 0;
if ([self.heightForView count]>0) {
    if (([[self.heightForView objectForKey:[NSString stringWithFormat:@"%i", indexPath.section+117]] floatValue]+20.0)>2009) {
        height = 2009;
        UIWebView *content = (UIWebView *)[self.view viewWithTag:(indexPath.section+117)];
        content.scrollView.scrollEnabled = YES;
    } else height = [[self.heightForView objectForKey:[NSString stringWithFormat:@"%i", indexPath.section+117]] floatValue]+20.0;
}
return height;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2015-08-28
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多