滚动tableview导致崩溃
问题描述:
我面临一个简单但烦人的内存释放和消息发送到释放对象的问题。滚动tableview导致崩溃
我正在尝试从网页获取Rss订阅源。我在第一阶段开发了两个阶段的小应用程序,因为我还不熟悉iphone开发,我试图从成功并没有问题的网页获取Rss源文本部分。在第2阶段,我尝试获取图像的链接,然后在每个UITableViewCell上显示它们并在之后显示详细视图。
我有一个加载rss的类,负责解析和返回通过[array copy]
返回的可变数组。一切运作良好,即使需要一些时间来加载。当运行应用程序,并尝试滚动表视图我得到一个崩溃和NSZombie消息:
objc [37372]:FREED(ID):消息保留 发送到释放对象= 0x3930cd0
这种情况也发生,当我尝试加载detailViewController没有滚动我发布了一些tableViewController看看有什么不对。
- (void)viewDidLoad {
[super viewDidLoad];
post = [[Posts alloc]init];
//self.tableView.rowHeight = 160.0;
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.navigationController.navigationBar.tintColor = [UIColor purpleColor];
NSLog(@"parser is about to init");
parser = [[XmlParser alloc] init];
array = [parser initWithURL:@"http://backend.deviantart.com/rss.xml?q=boost%3Apopular+meta%3Aall+max_age%3A8h&type=deviation&offset=0"];
//array = [parser initWithURL:@"http://www.enet.gr/rss?i=news.el.categories&c=politikh"];
NSLog(@"posts has %d items",[array count]);
[parser release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
post = [array objectAtIndex:indexPath.row];
// Set up the cell...
[cell.textLabel text:[post itemTitle]];
[cell.detailTextLabel setText:[post itemBody]];
[cell.imageView setImage:[self.post itemthumbnail]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
FirstDetailedViewController *anotherViewController = [[[FirstDetailedViewController alloc] initWithNibName:@"FirstDetailedViewController" bundle:nil]autorelease];
NSLog(@"posts count : %d", [array count]);
post = [array objectAtIndex:indexPath.row];
anotherViewController.currpost = [post retain];
[self.navigationController pushViewController:anotherViewController animated:YES];
}
的后冲突是一些NSMutableStrings和UIImages,所有的人都被分配在自己的init初始化,并在自己的dealloc方法dealloced。当我尝试在没有滚动的情况下点击某个单元格时,我也遇到了问题,但这是tommorows问题。
答
解决了! 简单的init问题
上一页:
-(id)init
{
[super init];
channelTitle = [[NSMutableString alloc]init];
channelLink = [[NSMutableString alloc]init];
channelDescription = [[NSMutableString alloc]init];
channelPubDate = [[NSMutableString alloc]init];
itemTitle = [[NSMutableString alloc]init];
itemBody = [[NSMutableString alloc]init];
itemPubDate = [[NSMutableString alloc]init];
itemLink = [[NSMutableString alloc]init];
itemthumbnail = [[UIImage alloc]init];
itemthumbnailLink = [[NSString alloc]init];
itemImage = [[UIImage alloc]init];
itemImageLink = [[NSString alloc]init];
return self;
}
现在:
-(id)init
{
if (self==[super init]){
channelTitle = [[NSMutableString alloc]init];
channelLink = [[NSMutableString alloc]init];
channelDescription = [[NSMutableString alloc]init];
channelPubDate = [[NSMutableString alloc]init];
itemTitle = [[NSMutableString alloc]init];
itemBody = [[NSMutableString alloc]init];
itemPubDate = [[NSMutableString alloc]init];
itemLink = [[NSMutableString alloc]init];
itemthumbnail = [[UIImage alloc]init];
itemthumbnailLink = [[NSString alloc]init];
itemImage = [[UIImage alloc]init];
itemImageLink = [[NSString alloc]init];
}
return self;
}
实际图像从未被分配过!现在它是! :D
答
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
post = [array objectAtIndex:indexPath.row];
// Set up the cell...
[cell.textLabel text:[post itemTitle]];
[cell.detailTextLabel setText:[post itemBody]];
[cell.imageView setImage:[self.post itemthumbnail]];
}
return cell;}
你不想在代码中的这个地方配置每个单元显示。您的代码应该看起来像这样:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
post = [array objectAtIndex:indexPath.row];
// Set up the cell...
[cell.textLabel text:[post itemTitle]];
[cell.detailTextLabel setText:[post itemBody]];
[cell.imageView setImage:[self.post itemthumbnail]];
return cell;
}
答
很可能是您的数据源过早自动释放的问题。尝试并保留array
对象或声明(保留)属性,并在你的viewDidLoad
方法:
NSArray *tmpArray = [parser initWithURL:@"http://backend.deviantart.com/rss.xml?q=boost%3Apopular+meta%3Aall+max_age%3A8h&type=deviation&offset=0"];
self.array = tmpArray;
如果希望帮助。 Rog
它看起来像这样,但我有同样的问题! 谢谢 – 2011-02-15 03:07:10
另外,你应该释放didSelectRowAtIndexPath – Olaf 2011-02-15 11:10:43