Reputation: 86057
Anyone got any ideas why this Table View code is crashing?
This, in my viewDidLoad
:
itemArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
Then, this in my cellForRowAtIndexPath
method:
cell.textLabel.text = [NSString stringWithFormat:@"Item (%@)", [itemArray objectAtIndex:indexPath.row]];
When I scroll down (i.e. so that object 1 goes off screen) then scroll back to try and see object 1, it crashes at this previous line.
It's fine if I replace the offending line with something like this:
cell.textLabel.text = @"test";
UPDATE: Answer was that the array was not being retained. This line fixed the problem:
itemArray = [[NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil] retain];
OR
itemArray = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];
Upvotes: 0
Views: 769
Reputation: 25632
Your itemArray seems not to be retained. arrayWithObjects:
returns an autoreleased object. You need to retain
or copy
it, or use the syntactic sugar of a retaining property.
Upvotes: 3