iphone:核心数据:NSManagedObjects和UITableView崩溃

iphone:核心数据:NSManagedObjects和UITableView崩溃,iphone,uitableview,core-data,nsmanagedobject,Iphone,Uitableview,Core Data,Nsmanagedobject,此获取方法工作正常,NSLog打印出数据库的内容…(在NSArray中获取对象): 。。但是,当我尝试填充tableView时(我已经构建了许多表,并且感觉我对它们的工作方式有很好的理解),程序会崩溃,出现“EXE_BAD_ACCESS at int retVal=UIApplicationMain(argc,argv,nil,nil);”即是说,没有其他错误消息 为了尝试和调试,我将这个简单的NSLog放在“cellforrowatinexpath”方法中,但它在NSLog(@“tablevi

此获取方法工作正常,NSLog打印出数据库的内容…(在NSArray中获取对象):

。。但是,当我尝试填充tableView时(我已经构建了许多表,并且感觉我对它们的工作方式有很好的理解),程序会崩溃,出现“EXE_BAD_ACCESS at int retVal=UIApplicationMain(argc,argv,nil,nil);”即是说,没有其他错误消息

为了尝试和调试,我将这个简单的NSLog放在“cellforrowatinexpath”方法中,但它在NSLog(@“tableview%I中的fetchedObjects计数,[fetchedObjects计数])时崩溃

据我所知,我还没有发布阵列

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]);
    NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
    NSLog(@"Name: %@", [info3 valueForKey:@"Name"]);
    cell.textLabel.text = [info3 valueForKey:@"Name"];

由于自动释放池刷新,您的
fetchedObjects
实例变量似乎正在被释放

NSManagedObjectContext
-executeFetchRequest:error:
方法返回一个自动删除的
NSArray
实例,您没有获得该实例的所有权,因此当执行到达
-tableView:cellforrowatinexpath:
方法时,自动删除池已被刷新,该对象被释放,现在有一个悬空指针。啊,经典

当然,解决方案是将
-retain
消息从发送
-executeFetchRequest:error:
发送到您的
NSManagedObjectContext
发送到返回的
NSArray


别忘了用
-dealoc
方法向它发送
-release
消息。

问题似乎出在这一行

NSManagedObject*info3=[fetchedObjects objectAtIndex:indexPath.row]

尝试使用

NSManagedObject*info3=[fetchedObjects对象索引:0]

因为UiTableViewCell计数和FetchedObject计数在整个过程中应该相等


如果不匹配,则会出现索引错误。

numberOfRowsInSection中的行数是多少?您使用哪种方法初始化FetchedObject?您好。我使用
return[ArrayNameHere count]目前是27。这意味着阵列就在那里。。。!?!谢谢你,雅各布。我将fetchObjects复制到另一个数组中:
displayArray=[NSArray arrayWithArray:fetchedObject]并打印出内容(一切正常)。在调用表reloaddata[displayArray count]之前,我检查了数组,这也非常有效。已将tableView更改为指向此新阵列,但它仍然在NSLog`NSLog(@“tableView%i中的displayArray计数,[displayArray计数]);`我甚至还手动创建了另一个数组,并将tableView指向该数组,以确保该表能够正常工作(而且确实如此)。为了以防万一,我还把这个项目清理了好几次。最后它终于开始工作了。我应该将
fetchedObjects
复制到一个新数组中,因此:
displayArray=[fetchedObjects mutableCopy]
似乎使用
[NSArray arrayWithArray:FetchedObject]
复制数组不正确。谢谢您的帮助。@Jeremy创建副本比简单地调用
-retain
要贵得多。你真的应该照我说的做,不要复制。这是不必要的开销。
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    NSLog(@"fetchedObjects count in tableview %i",[fetchedObjects count]);
    NSManagedObject *info3 = [fetchedObjects objectAtIndex:indexPath.row];
    NSLog(@"Name: %@", [info3 valueForKey:@"Name"]);
    cell.textLabel.text = [info3 valueForKey:@"Name"];