views:

62

answers:

2

I have a table view with tall cells. About 300 in height. The cells are custom and have an associated nib. In cellForRowAtIndexPath, I access an object in an array using indexPath.row. The object has properties, which I assign to labels on the custom cell. This works fine for the first two cells. Once I scroll enough for the third cell to get created, the app crashes.

I have NSZombieEnabled set and here is the output:

2010-04-19 21:48:13.360 MyApp[54463:207] *** -[CALayer release]: message sent to deallocated instance 0xfc4e50
(gdb) continue
2010-04-19 21:48:18.382 MyApp[54463:207] *** NSInvocation: warning: object 0xfc4e50 of class '_NSZombie_CALayer' does not implement methodSignatureForSelector: -- trouble ahead
2010-04-19 21:48:18.383 MyApp[54463:207] *** NSInvocation: warning: object 0xfc4e50 of class '_NSZombie_CALayer' does not implement doesNotRecognizeSelector: -- abort
(gdb) continue
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) 

I'm not sure what is deallocated. How can I trace this to the source?

-- EDIT --

Adding cellForRowAtIndexPath implementation:

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

static NSString *CellIdentifier = @"Cell";

OrderCell *cell = (OrderCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OrderCellView" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

// Set up the cell...
if(self.rows != nil){
    id obj = [self.rows objectAtIndex:indexPath.row];
    cell.orderId.text = [NSString stringWithFormat:@"%@",[obj objectForKey:@"OrderId"]];
    cell.orderName.text = [NSString stringWithFormat:@"%@",[obj objectForKey:@"OrderName"]];
}

return cell;
}
+1  A: 

You need to read the documentation of loadNibNamed:owner:options:. The answer to your problem is given there:

You should retain either the returned array or the objects it contains manually to prevent the nib file objects from being released prematurely.

I would make an instance variable and property for the array returned by the load method and make sure the array is retained, otherwise it and all of the objects in it will disappear at the end of the current event when the autorelease pool is drained.

JeremyP
I created @property(nonatomic, retain) NSArray *CellViewNib; and used self.CellViewNib in cellForRowAtIndexPath. It did not make any difference.
4thSpace
A: 

Here is the solution:

As mentioned in the OP, this is a custom cell and loads a nib. I'm using

static NSString *CellIdentifier = @"Cell";

but did not specify anything in IB for the identifier. It was blank. After putting in "Cell" for the identifier, it now works without issue. I'm guessing the nib was being deallocated from the cell object created in code, since one had an identifier and the other didn't.

I'd be interested in the details of why the IB identifier is also so important for the cell in this case...if anyone may be able to provide some details.

4thSpace