繁体   English   中英

加载UIWebView时出现UITableViewCell错误

[英]UITableViewCell error loading UIWebView

屏幕截图

屏幕截图

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

也许UITableViewCell的最大高度是多少?

这是另一个错误?

我能做什么?

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

#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

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

我在这里找到了答案(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;

暂无
暂无

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

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