开发者

TableViewController - Retrieve data from Database

I've a problem with my TableController.

When I call cellForRowAtIndexPath() I'm able to retrieve info from Database class with:

myGroupsAppDelegate *appDelegate = (myGroupsAppDelegate *)[[UIApplication sharedApplication] delegate]; 
myGroupsDB *mgdb = [ appDelegate.groups_db objectAtIndex:开发者_如何学PythonindexPath.row ];   
cell.textLabel.text = mgdb.group_name;
[mgdb release];

but if I try to call the same from didSelectRowAtIndexPath() I get an error. I need to pass two values to a view and I'm using this code:

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

When I try to assign mgdb.id_group to sgv.groupID I get a EXC_BAD_ACCESS. It seems that mgdb is empty.

-------------- CHANGE ------------------

After a Build and Analyze, compiler shown "Incorect decrement of the reference count of an object that is no owner at this point". But, I create here, isn't it local? So I added a retain:

myGroupsDB *mgdb = [[ appDelegate.groups_db objectAtIndex:indexPath.row ] retain];

and now it works, but, if I try to recall the same code (just pressing on row, change view, come back and press row again) the application chrash without log and with the same message.

Any suggestion?

thanks, Andrea


It seems that you are over-releasing the mgdb object.

If you don't get the object from a method that's name begins with alloc, new and if you don't send a retain message to it, then it means that you don't "own" the object and you should not release it.

You can read more in the Memory Management Programming Guide.

Note: when you get EXC_BAD_ACCESS it means that you are accessing a memory zone that you are not allowed to (in this case you are not allowed to access it because it probably has been dealloc-ed).


I found (me? you) the solution.

cellForRowAtIndexPath() is:

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

cell.textLabel.text = mgdb.group_name;

and didSelectRowAtIndexPath() is:

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

I removed the both [release] because, as described on Memory Managment Programming Guide, I don't own the mgdb instance so I'm not responsible to reliquishing ownership. So, I don't know why it's not mandatory to create a new instance on memory with alloc or newObject but, if I'm not the owner I've not to release the object. I also add the casting of mgdb, not necessary but good practice.

bye, Andrea

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜