【问题标题】:Memory leaks in UITableViewUITableView 内存泄漏
【发布时间】:2011-07-20 10:26:57
【问题描述】:

我正在使用自定义单元格来填充我的一张表格。但是每当我向上和向下滚动时,我都可以看到发生了一些泄漏,这些泄漏指向 UIKit Lib。不知道为什么?作为参考,我附上了泄漏仪器的泄漏屏幕截图。

感谢您的帮助!!

TableView cellForRowIndex:

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

    NSString *cellIdentifier = [NSString stringWithFormat:@"EditingCell_%d_%d",indexPath.section,indexPath.row];
    EditingTableViewCell *cell = (EditingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        [self.appCellNib instantiateWithOwner:self options:nil];        
        cell = editingTableViewCell;        
        self.editingTableViewCell = nil;
    }

自定义单元格:

@implementation EditingTableViewCell

@synthesize label,textField,commentField,dateLabel;

#pragma mark -
#pragma mark == Initialization == 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];
}

#pragma mark -
#pragma mark == Memory Management ==

- (void)dealloc {
    [label release];
    [textField release];
    [commentField release];
    [dateLabel release];
    [super dealloc];
}

【问题讨论】:

  • 最有趣的部分一定在tableView:cellForRowAtIndexPath:方法中
  • @Vince:我已经交叉检查了好几次,但一切都很好。
  • 请将您的表格单元创建代码放在这里
  • @manoj - 所以,很抱歉,不知道为什么会出现泄漏
  • 请在此处提供有关您的 tableView:cellForRowAtIndexPath 代码的更多详细信息。所以,可以为您提供更多帮助。

标签: iphone memory memory-leaks


【解决方案1】:

我不确定是否有足够的信息来提供精确的解决方案。但是,我的猜测是这些属性没有很好地定义用于膨胀的 xib。例如,您可能已将属性 label 定义为:

// Interface declaration
IBOutlet UILabel *label;

// property 

@property (nonatomic, retain) UILabel *label;

这是错误的,会导致泄漏。定义网点的正确方法是:

// Interface declaration
UILabel *label;

// property 
@property (nonatomic, retain) IBOutlet UILabel *label;

这使得当 xib 被夸大时,变量的设置通过综合设置器,因此内存管理按您预期的方式工作。

希望对您有所帮助!干杯

【讨论】:

  • 这不是问题,我总是喜欢声明像第二个这样的属性。
猜你喜欢
  • 2013-12-24
  • 2012-07-09
  • 2011-10-23
  • 2013-11-06
  • 2011-06-03
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多