views:

44

answers:

2

Hi, my problem is that I can't access my NSMutableArray in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}.

I create my NSMutableArray here:

nodes = [xmlDoc nodesForXPath:@"/xml/items/item/short_desc" error:nil];

if (nodes != nil && [nodes count] >= 1) {
    for (int i = 0; i < [nodes count]; i++) {
        CXMLElement *resultElement = [nodes objectAtIndex:i];
        result = [[[[resultElement attributeForName:@"data"] stringValue] copy] autorelease];       
        [short_desc addObject:result];
    }
}

and I can print out the content of short_desc everywhere with:

NSLog([short_desc objectAtIndex:0]);

but not in (if I do so, my app crashes):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

.....

NSString *date = [name objectAtIndex:0];


labelDate.text = date;
.....   

return cell;}

if I use:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

.....

NSString *date = @"text...";


labelDate.text = date;
.....   

return cell;}

it works correctly.

ANY SOLUTION FOR THIS PROBLEM???

+2  A: 

You dont show how you create the array, but the most likely cause is that you are not retaining your array and its being released before u hit the method that crashes. How are you creating the name array?.. Right so you see you are creating the array like so 62. name = [NSMutableArray arrayWithCapacity:10]; you need to retain the array otherwise the OS will release the memory and you wont bea ble to access it...instead you can do

name =[ [NSMutableArray arrayWithCapacity:10] retain];

The poster below me posted an article about memory managment you should go over that...here is a link anyway memory managment guide

Daniel
NSMutableArray *short_desc;short_desc = [NSMutableArray arrayWithCapacity:10];
pbcoder
Here is all my code: http://pastebin.com/iFKb6BWQ
pbcoder
right, check out my edit
Daniel
A: 

If you create array using using methods with "alloc", "new", or "copy" word in method name - you don't need to retain array. In other cases created array is authoreleased array. If you want to store it - you should increase retain counter by sending retain method (e.g. [names retain]). And don;t forget to release it when you finish using it (e.g. in dealloc method).

Please read Memory Management Guide.

OgreSwamp
short_desc = [NSMutableArray arrayWithCapacity:10]; returns autoreleased array. use:short_desc = [NSMutableArray arrayWithCapacity:10];[short_desc retain];
OgreSwamp
for your code (from link) you should create arrays like that:short_desc = [[NSMutableArray alloc]initWithCapacity:10];name = [[NSMutableArray alloc]initWithCapacity:10];location = [[NSMutableArray alloc]initWithCapacity:10];startDate = [[NSMutableArray alloc]initWithCapacity:10];and in dealloc method you should do:[short_desc release];[name release];etc...
OgreSwamp
short_desc = [[NSMutableArray alloc]initWithCapacity:10]; worked fine! Thanks!
pbcoder