Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c TableViewController-从数据库检索数据_Objective C_Uitableview - Fatal编程技术网

Objective c TableViewController-从数据库检索数据

Objective c TableViewController-从数据库检索数据,objective-c,uitableview,Objective C,Uitableview,我的桌面控制器有问题 调用CellForRowatineXpath()时,我可以使用以下命令从数据库类检索信息: myGroupsAppDelegate *appDelegate = (myGroupsAppDelegate *)[[UIApplication sharedApplication] delegate]; myGroupsDB *mgdb = [ appDelegate.groups_db objectAtIndex:indexPath.row ]; cell.textLa

我的桌面控制器有问题

调用CellForRowatineXpath()时,我可以使用以下命令从数据库类检索信息:

myGroupsAppDelegate *appDelegate = (myGroupsAppDelegate *)[[UIApplication sharedApplication] delegate]; 
myGroupsDB *mgdb = [ appDelegate.groups_db objectAtIndex:indexPath.row ];   
cell.textLabel.text = mgdb.group_name;
[mgdb release];
但是,如果我尝试从didSelectRowAtIndexPath()调用相同的函数,我会得到一个错误。我需要向一个视图传递两个值,我使用以下代码:

SingleGroupView *sgv = [[SingleGroupView alloc] initWithNibName:@"SingleGroupView" bundle:[NSBundle mainBundle]];

myGroupsAppDelegate *appDelegate = (myGroupsAppDelegate *)[[UIApplication sharedApplication] delegate];
myGroupsDB *mgdb = [ appDelegate.groups_db objectAtIndex:indexPath.row ];

sgv.groupID = mgdb.id_group;
sgv.groupName = mgdb.group_name;

[mgdb release];
当我尝试将mgdb.id_group分配给sgv.groupID时,我获得了EXC_BAD_访问权限。mgdb似乎是空的

--------------改变------------------

在构建和分析之后,编译器显示“此时没有所有者的对象的引用计数的非直接递减”。但是,我在这里创作,不是本地的吗?因此,我添加了一个保留:

myGroupsDB *mgdb = [[ appDelegate.groups_db objectAtIndex:indexPath.row ] retain];
现在它可以工作了,但是,如果我尝试调用相同的代码(只需按一下行,更改视图,然后返回并再次按一下行),应用程序chrash没有日志,并且带有相同的消息

有什么建议吗

谢谢,
Andrea

您似乎过度释放了
mgdb
对象

如果您没有从名称以
alloc
new
开头的方法获取对象,并且没有向其发送
retain
消息,则表示您不“拥有”该对象,也不应
释放它

你可以在网上阅读更多

注意:当您获得
EXC\u BAD\u ACCESS
时,表示您正在访问一个不允许访问的内存区域(在这种情况下,您不允许访问它,因为它可能已被解除锁定)。

我找到了(我?您)解决方案

CellForRowatineXpath()是:

而didSelectRowatineXpath()是:

我删除了这两个[release],因为正如《内存管理编程指南》中所述,我不拥有mgdb实例,因此我不负责重新获得所有权。所以,我不知道为什么使用alloc或newObject在内存上创建一个新实例不是强制性的,但是,如果我不是所有者,我就不会释放该对象。我还添加了mgdb的铸造,这不是必需的,而是良好的实践

再见, 安德烈

myGroupsAppDelegate *appDelegate = (myGroupsAppDelegate *)[[UIApplication sharedApplication] delegate];
myGroupsDB *mgdb = (myGroupsDB*)[ appDelegate.groups_db objectAtIndex:indexPath.row ];

cell.textLabel.text = mgdb.group_name;
myGroupsAppDelegate *appDelegate = (myGroupsAppDelegate *)[[UIApplication sharedApplication] delegate];
myGroupsDB *mgdb = (myGroupsDB *)[appDelegate.groups_db objectAtIndex:indexPath.row];