滚动tableview导致崩溃

时间:2011-02-14 23:43:53

标签: iphone uitableview uiimage reuseidentifier

我面临着一个简单但烦人的内存释放和消息发送到释放对象的问题。

我正在尝试从网页上获取Rss Feed。我在第一阶段开发了两个阶段的小程序,因为我还不熟悉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方法中解除分配。当我尝试在没有滚动的情况下点击一个单元格但是这是tommorows问题时,我也遇到了问题。

3 个答案:

答案 0 :(得分:1)

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;
}

答案 1 :(得分:1)

解决了! 简单初始化问题

上一篇:

    -(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

答案 2 :(得分:0)

很可能是您的数据源过早自动释放的问题。尝试并保留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