Ios UITableViewCell加载UIWebView时出错

Ios UITableViewCell加载UIWebView时出错,ios,xcode,uitableview,uiwebview,Ios,Xcode,Uitableview,Uiwebview,正如你所看到的,有一个错误。我正在每个UITableViewCell上加载UIWebView。问题是,当加载大量数据时,UITableViewCell会消失,当我滚动文本覆盖层时,它也会消失 UITableViewCell可能有一个最大高度 这是另一个错误 我能做什么 编辑:这是viewController代码,用于管理API响应,并将每个“post”存储为UIWebView,加载到主视图和UITableViewCell内容视图中。问题是:大型网络视图的问题在哪里?密码?最大高度?记忆 #imp

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

UITableViewCell可能有一个最大高度

这是另一个错误

我能做什么

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

#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
#导入“TopicViewController.h”
#导入“HTMLController.h”
NSString*_topicID;
@实现主题视图控制器
@综合高度前视=_高度前视;
@综合tableView=\u tableView;
@合成htmlController=\u htmlController;
@综合分页计数器=_分页计数器;
+(void)setTopicID:(NSString*)topicID{
_topicID=topicID;
}
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
}
回归自我;
}
-(无效)未收到记忆警告
{    
[超级记忆警告];
}
#pragma标记-视图生命周期
-(无效)viewDidLoad
{
[超级视图下载];
self.heightForView=[[NSMutableDictionary alloc]init];
self.htmlController=[[htmlController alloc]init];
[self.htmlController requestTopicWithID:_topicID];
NSInteger viewTag=117;
for(self.htmlController.postsContent中的NSString*html){
NSString*formattedHTML=[NSString stringWithFormat:@“%@”,html];
NSLog(@“%@”,格式化HTML);
UIWebView*content=[[UIWebView alloc]initWithFrame:CGRectMake(0,0300,1)];
content.tag=viewTag;
content.alpha=0;
content.不透明=否;
content.backgroundColor=[UIColor clearColor];
content.scrollView.scrollEnabled=否;
content.scalesPageToFit=否;
content.delegate=self;
[self.view addSubview:content];
[内容加载HtmlString:FormattedHtmlBaseURL:[NSURL URLWithString:@]http://www.elotrolado.net"]]; 
NSLog(@“加载wv%i”,视图标签);
viewTag++;
}
}
-(无效)视图卸载
{
[超级视频下载];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation
{
返回(interfaceOrientation==UIInterfaceOrientationGraphic);
}
#pragma标记-TableViewDelegate和数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回[self.heightForView计数]>0?[self.heightForView计数]:0;
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分{
返回零;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回1;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
返回[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”,indexath.section];
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(!单元格){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier];
UIView*content=[self.view viewWithTag:(indexPath.section+117)];
content.alpha=1;
[cell.contentView addSubview:content];
}
返回单元;
}
-(无效)webViewDidFinishLoad:(UIWebView*)webView{
CGRect frame=webView.frame;
frame.size=[webView-sizehatfits: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(“所有wv加载”);
[self.tableView重载数据];
}else NSLog(@“wv%i已加载”,webView.tag);
} 
@结束
此代码适用于大多数Web视图,但不适用于大型Web视图

我找到了答案(tableView:HeightForRowAtIndexPath:documentation)

重要信息由于基础实现细节,您不应返回大于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;

你也可以发布相关代码吗?我不知道发布什么,所以我将发布viewControllermaybe的全部代码。这与此无关,但你不应该为每个单元格使用不同的单元格标识符。您正在阻止单元格被重用,如果您有很多单元格,这可能会导致严重的内存问题。您可能希望将
UITableViewCell
子类化,并向其添加
UIWebView
,然后在其中更改大小。您是否还在
viewDidLoad
加载所有Web视图?您可能希望延迟加载它们。使用唯一标识符的目的是避免重新加载Web视图。单元格的数量是静态的,始终为10。无论如何,谢谢=)