Ngoan Nguyen
Ngoan Nguyen

Reputation: 747

Why is my NSMutableArray deallocating in UITableView

My NSMutableArray seems to be releasing and I can't figure out where, here is my code:

- (void)awakeFromNib {

arrayOfData = [[NSMutableArray alloc] initWithCapacity:0];
[arrayOfData addObject:@"test"];

NSLog(@"array: %@", arrayOfData); 

}

All is well here!

Then I create a UITable and add it to the view, then I request some data, everything is normal.

        arrayOfData = [response objectForKey:@"results"];
        NSLog(@"count: %i", [arrayOfData count]);
        NSLog(@"success!");
        numberOfRows = [arrayOfData count];
        [self.myTableView reloadData];

Creating the number of rows works fine too:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSLog(@"test: %i", numberOfRows);
return numberOfRows;

}

Now accessing my array gets a "message sent to deallocated instance" at that line.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier] autorelease];
}


   // crashes here!
NSLog(@"array: %i", [arrayOfData count]);

if (numberOfRows){

    NSString *filename = [[arrayOfData objectAtIndex:indexPath.row] objectForKey:@"filename"];
    NSString *url  = [NSString stringWithFormat: @"http://www.web.com/dir/%@", filename];  
    [cell.imageView setImageWithURL:[NSURL URLWithString:url]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

    cell.textLabel.text = @"My Text";
}

return cell;

}

I have also have this in my header, and I used @synthesize as well

@property (nonatomic, retain) NSMutableArray *arrayOfData;

Any help would be appreciated!

Upvotes: 1

Views: 250

Answers (1)

paulmelnikow
paulmelnikow

Reputation: 17208

It looks like you're reassigning arrayOfData with this line:

arrayOfData = [response objectForKey:@"results"];

If that's what you want, try this instead:

arrayOfData = [[response objectForKey:@"results"] retain];

Upvotes: 1

Related Questions