Anders
Anders

Reputation: 23

Why is my NSMutableArray "half dead" in cellForRowAtIndexPath?

In rootViewController.h i have a property NSMutableArray mainList:

@interface RootViewController : UITableViewController {    
    NSMutableArray *mainList;
}
@property (nonatomic, retain) NSMutableArray *mainList;
@property (nonatomic, retain) IBOutlet DetailsViewController *detailsController;

In the m file, when loading, the following works just fine:

- (void)viewDidLoad
{
self.mainList = [[NSArray alloc] initWithObjects:@"Country1", @"Contry2", nil];
}

Populating the array by a class also works fine (on first load):

innehall *myInnehall= [[innehall alloc] init];
[myInnehall fillMain];
self.mainList = myInnehall.theMainList;
[myInnehall release];

(the debugger shows data to be correct in the array)

When scrolling, the app crasches at setting the label of the cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.mainList objectAtIndex: [indexPath row]];
return cell;
}

In the debugger, the Array is only populated 1-9 instead of up to 19. 10-19 containing strange objects. What can be eating my Array??

Upvotes: 2

Views: 361

Answers (2)

Felipe Sabino
Felipe Sabino

Reputation: 18225

First of all, your NSMutableArray property must be initialized properly, like:

self.mainList = [[NSMutableArray alloc] init];

(Not as a NSArray)

Then, you are making your property mainList point to myInnehall.theMainList and you are releasing it afterwards, that is what is causing the crash.

Try just add the myInnehall items to your mainList

[self.mainList addObjectsFromArray:myInnehall.theMainList];

Upvotes: 2

akashivskyy
akashivskyy

Reputation: 45210

Try to change

self.mainList = myInnehall.theMainList;

to

self.mainList = [myInnehall.theMainList copy];

Can you also put NSLog([self.mainList description]) in your cellForRowAtIndexPath and post the result?


PS: You have NSMutableArray in property declaration and you initialize it as NSArray?

Upvotes: 0

Related Questions