Home  >  Q&A  >  body text

objective-c - iOS中UIView调用removeFromSuperView之后没有释放内存

我有一个自定义的UITableViewCell,暂且叫ACell,该cell有以下属性:

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *deleteButton;

全部addSubview到cell的contentView,主要是点击deleteButton之后把titleLabel移除:

- (void)deleteButtonClicked {
    [self.titleLabel removeFromSuperview];
}

执行以上语句之后我发现,titleLabel还是存在于内存中。就像下面这样:

怎么样才能让它立即销毁

伊谢尔伦伊谢尔伦2761 days ago1080

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-18 09:42:45

    The reason is: ACell still holds the titleLabel object.
    Solution:self.titleLabel = nil;

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:42:45

    Replace strong with weak. One thing to note is to copy the attributes after addsubview

    reply
    0
  • Cancelreply