0

I have a view controller (EmbeddedMenuView) that uses a custom view (HorizontalMenuView). The Embedded menu view uses multible HorizontalMenuViews. The HorizontalMenuView contains a UITableView. Each cell in the table view uses quite a bit of memory (high quality images.).

Now, I need to execute a task every time a section of the table view cells in the HorizontalMenuView is touched. I did this by creating a protocol in the table view cell and assigning the HorizontalMenuView its delegate. Then I created a protocol in the HorizontalMenuView and assigned the EmbeddedMenuView its delegate. So I pass the touch event up to the EmbeddedMenuView.

The problem is, when I assign the cell's delegate, the HorizontalMenuView does not get deallocated. Since this view refreshes itself every time the view appears, the memory footprint gets out of control fast.

If I comment out the part where the cell is assigned a delegate, everything works fine.

My question is: How can I properly release a UITableViewCell's delegate?

This is the code snippet from the HorizontalMenuView:

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

//Custom Logic

    HorizontalMenuItemTableViewCell *cell = (HorizontalMenuItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {

        cell = [[[NSClassFromString([[AMPUIManager sharedManager] classNameForName:cellIdentifier]) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];

        cell.shouldAlwaysTransform = shouldAlwaysTransform;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.colorsDict = colorsDict;

        if ([cell isKindOfClass:[ATCustomTableViewCell class]]) {
            ((ATCustomTableViewCell *)cell).delegate = self; //Commenting this out solves my problem.
        }

    }

//More Custom Logic 



    return cell;

}

PS I am using manual reference counting. ARC is not an option for this project.

scord
  • 1,375
  • 1
  • 17
  • 34
  • smells a little like a circular reference - are you using 'assign' convention for your delegates? – WiseOldDuck Aug 27 '12 at 00:10
  • I was using retain. I seemed to have overlooked that detail. Make this an answer and I will accept it if it checks out. – scord Aug 27 '12 at 00:13

1 Answers1

1

It sounds like you may have a circular reference. You almost always want to use 'assign' convention with delegates.

See: Why are Objective-C delegates usually given the property assign instead of retain?

Community
  • 1
  • 1
WiseOldDuck
  • 3,156
  • 2
  • 24
  • 27