How do I fix the problem of freeing an object in uitableview?

I have a table view with tall cells. About 300 in height. The cells are conventional and have an associated tip. In cellForRowAtIndexPath, I access the object in the array using indexPath.row. The object has properties that I assign to the labels on the custom cell. This works great for the first two cells. As soon as I scroll enough to create the third cell, the app crashes.

I have NSZombieEnabled installed 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 exempt. How can I trace this back to the source?

- EDIT -

Adding the 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;
}

      

+2


a source to share


2 answers


Here's the solution:

As OP mentioned, this is a custom cell and loads the tip. I use

static NSString *CellIdentifier = @"Cell";

      



but didn't specify anything in IB for the identifier. It was empty. After entering "Cell" for the ID, it now works without issue. I am assuming the nib was freed from the cell object created in the code as it had an id and the other did not.

I would be interested to know why the IB ID is also so important to the cell in this case ... if someone can provide some details.

+2


a source


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

You must save either the returned array or the objects it contains manually to prevent premature release of the nib file objects.



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

+1


a source







All Articles