【问题标题】:UIView & UITableViewCell not being deallocated when UIView becomes the delegate of a UITableViewCell当 UIView 成为 UITableViewCell 的委托时,UIView 和 UITableViewCell 没有被释放
【发布时间】:2012-08-21 12:26:20
【问题描述】:

我有一个使用自定义视图 (Horizo​​ntalMenuView) 的视图控制器 (EmbeddedMenuView)。 Embedded 菜单视图使用多个 Horizo​​ntalMenuViews。 Horizo​​ntalMenuView 包含一个 UITableView。表格视图中的每个单元格都使用相当多的内存(高质量图像)。

现在,每次触摸 Horizo​​ntalMenuView 中的表格视图单元格的一部分时,我都需要执行一项任务。我通过在表格视图单元格中创建一个协议并为 Horizo​​ntalMenuView 分配其委托来做到这一点。然后我在 Horizo​​ntalMenuView 中创建了一个协议,并为 EmbeddedMenuView 分配了它的委托。所以我将触摸事件传递给 EmbeddedMenuView。

问题是,当我分配单元格的委托时,Horizo​​ntalMenuView 不会被释放。由于该视图每次出现时都会自行刷新,因此内存占用很快就会失控。

如果我注释掉单元格被分配代表的部分,一切正常。

我的问题是:如何正确释放 UITableViewCell 的委托?

这是来自 Horizo​​ntalMenuView 的代码 sn-p:

-(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 我正在使用手动引用计数。 ARC 不是此项目的选项。

【问题讨论】:

  • 闻起来有点像循环引用 - 您是否为您的代表使用“分配”约定?
  • 我使用的是保留。我似乎忽略了这个细节。将此作为答案,如果签出,我会接受。

标签: ios ipad memory-management uitableview


【解决方案1】:

听起来你可能有一个循环引用。您几乎总是希望对代表使用“分配”约定。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多