views:

198

answers:

1

Hi all, I'm trying to show a database information in a tableview and then the datailed information in a view my problem is as follow:

I created a NSMutable array: NSMutableArray *myArray = [[NSMutableArray alloc]init]; Then I populated it form a database MyObj *obj = [[MyObj alloc]initWithName:idObj];

When I show the values in the tableview "cellForRowAtIndexPath" method everything goes fine, but when I try to load the same array in the "didSelectRowAtIndexPath" method, the values in the arrays are "invalid".

The NSMutableArray declaration is as follow: @property (nonatomic, retain) NSMutableArray *myArray;

Could someone help me !!!

Here is the code:

.h file

NSMutableArray *antrosArray; @property(nonatomic, retain) NSMutableArray *antrosArray;

.m file

@synthesize antrosArray;

filling the array:

Antro *antro = [[Antro alloc]initWithName:idAntro nombre:aName]; [self.antrosArray addObject:antro];

my problem is:

when I access the array from the cellForRowAtIndexPath tableView methods, everything goes ok, but when the user selects a record I tried to read the array in the didSelectRowAtIndexPath method and all the values in my array are invalid.

The array is there, and also the object, but the values for the objects are empty, the cellForRowAtIndexPath method is cleaning the array.

Hope this is more clear.

Thanks

+3  A: 

It seems that you are defining a second local variable called myArray. What you should do instead is

self.myArray = [[[NSMutableArray alloc] init] autorelease];

As a matter of fact, you should probably be using -initWithCapacity: instead as well, but that's another matter.

Edit: Thanks Chuck for pointing out my missing autorelease :)

Magnus Nordlander
Actually, it should be either `[[[NSMutableArray alloc] init] autorelease]` or one of the convenience constructors, because the setter already claims ownership. Without the release, you get a memory leak.
Chuck
Thanks for your answer, I tried your solution, but I'm still getting invalid values in my array when I access it from another method. I read some about memory management, and it suggest that my arrays it is been realeased automatically. I used "retain" to keep the information but it doesn't work.
Guillermo Palacios
@Guillermo: Please edit your question to include a minimal example including Magnus' suggestions but still showing the bug. Without anything concrete to look at, we're stabbing blindly in the dark.
Chuck