Hello all -
I am learning Obj-C but still occasionally have a difficult time wrapping my head around some of the mem management stuff. I am using custom cells with a UITableView, and implemented the cellForRowAtIndexPath method where I accidentally released the cell at the end. This obviously caused problems as the cell was also getting released when the tableView was popped. This led to a crash due to releasing the cell twice - no prob, understand.
However, as I kept working, I intermingled standard and custom cells, so my method got a bit more complex. My first try was the below, which caused the same problem as the above scenario. This is where I am a bit confused - since I am not releasing "cell", why can't I release "customCell" after I set cell to its value?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
//CATEGORY_SECTION is a constant defined elsewhere
if (indexPath.section == CATEGORY_SECTION) {
cell = [tableView dequeueReusableCellWithIdentifier:@"StandardCellIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StandardCellIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.labelText.text = myModelObject.name;
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier"];
MyCustomCellClass *customCell;
if (cell == nil) {
UIViewController *helperController = [[UIViewController alloc] initWithNibName:@"MyCustomCell" bundle:nil];
customCell = (MyCustomCellClass *)[helperController view];
[helperController release];
}
customCell.myCustomLabel.text = myModelObject.description;
cell = customCell;
[customCell release];
}
return cell;
}
I understand that when I set cell = customCell, I am assigning customCell's memory address, not the actual object, to cell...so when I release customCell it is also in effect releasing cell? How would I actually copy customCell so I could release it? Or do I not have to release it (even though I alloc'd it) - it seems like a memory leak waiting to happen, how would you approach it?
For the record, here is the revised code I used to avoid this issue. This question isn't so much as finding a solution (since I have, below) but understanding what is happening behind the scenes. Thanks for any guidance!
My working method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//CATEGORY_SECTION is a constant defined elsewhere
if (indexPath.section == CATEGORY_SECTION) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StandardCellIdentifier"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StandardCellIdentifier"] autorelease];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.labelText.text = myModelObject.name;
return cell;
} else {
MyCustomCell *customCell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellIdentifier"];
if (customCell == nil) {
UIViewController *helperController = [[UIViewController alloc] initWithNibName:@"MyCustomCell" bundle:nil];
customCell = (MyCustomCell *)[helperController view];
[helperController release];
}
customCell.myCustomLabel.text = myModelObject.description;
return customCell;
}
}