0

屏幕截图

屏幕截图

如您所见,有一个错误。我在每个 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

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

4

1 回答 1

0

我在这里找到了答案(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;
于 2012-08-03T02:22:55.523 回答