2012-08-03 66 views
0

Screen capture的UITableViewCell错误加载一个UIWebView

Screen capture

正如你可以看到有一个错误。我在每个UITableViewCell上加载一个UIWebView。问题是,当它加载大量的数据时,UITableViewCell消失,当我滚动文本覆盖本身。

也许有一个UITableViewCell的最大高度?

这是另一个错误?

我该怎么办?

编辑:这是管理API响应和存储每个“交”作为一个UIWebView所述的viewController代码,至极被加载在主视图和的UITableViewCell内容查看。问题是:大型webViews的问题在哪里?码?最大高度?记忆?

#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 

此代码适用于大多数网页视图,但它与路数

+0

你可以发布相关的代码吗? – aforaudrey 2012-08-03 00:35:32

+0

好吧,我不知道要发布什么,所以我会发布viewController的整个代码 – xXDraklordXx 2012-08-03 00:47:59

+0

也许这是不相关的,但你不应该为每个单元格使用不同的单元格标识符。您正在防止细胞被重复使用,并且如果您有很多细胞,这可能会导致严重的内存问题。你可能想要子类UITableViewCell并添加一个UIWebView到它,然后在那里改变它的大小。你是否也加载了'viewDidLoad'的所有webviews?您可能想要懒惰地加载它们.. – aforaudrey 2012-08-03 01:21:38

回答

0

我已经找到了答案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;