Access problem with NSMutableArray
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 < [开发者_Go百科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???
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
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.
精彩评论