gpt4 book ai didi

objective-c - 应用程序因错误 : modifying layer that is being finalized 而崩溃

转载 作者:行者123 更新时间:2023-11-28 20:43:42 26 4
gpt4 key购买 nike

我的应用程序在滚动 tableview 后崩溃并显示消息“正在修改正在完成的图层”。

我认为错误是由于我在该方法结束时释放了“videoView”(代码的倒数第二行)。

我该如何解决这个问题?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = @"PlaceholderCell";

GenericObject *youTubeVid = [self.searchResultArray objectAtIndex:indexPath.row];

UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier]autorelease];

}
UIWebView *videoView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 104, 104)];

NSString *cellid=[NSString stringWithFormat:@"Cell%i%i", indexPath.section, indexPath.row];

if([self.webViewCache objectForKey:cellid])
{
videoView=[self.webViewCache objectForKey:cellid];
}
else
{
NSString *url = [NSString stringWithFormat:@"http://www.youtube.com/watch?v=%@",youTubeVid.vid];
NSString *videoHTML = [self embedYouTube:url frame:CGRectMake(0, 0, 104, 104)];
[videoView loadHTMLString:videoHTML baseURL:nil];
[self.webViewCache setObject:videoView forKey:cellid]; //Save webview in dictionary
}

[cell.contentView addSubview:videoView];

//Error seems to be here
[videoView release];

return cell;
}

最佳答案

错误是因为线路,

if([self.webViewCache objectForKey:cellid])
{
videoView=[self.webViewCache objectForKey:cellid];
}

这里你只是从字典中获取 WebView ,它显然返回一个 autoreleased 对象。因此,当您尝试释放它时(不知道它是否已分配或刚刚从字典中获取)会发生错误。

一种解决方案是保留 videoView

videoView=[[self.webViewCache objectForKey:cellid] retain];

关于objective-c - 应用程序因错误 : modifying layer that is being finalized 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7316903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com