0

我在启用 ARC 的新应用程序中的 cellForRowAtIndexPath 中出现内存泄漏。cellForRowAtIndexPath 只显示一个 UILabel。我添加了 [myUIlabel release]; 我收到 ARC 错误:“ARC 禁止发送‘release’的显式消息”

如果我删除 UILabel,泄漏就会消失。

我不想禁用 ARC,因为它使内存管理。更轻松。

解决办法是什么?

这是代码...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int   row = indexPath.row;
    float font_size;
    UITextView*  PWR_RX_cover_box;
    int x,y,w,h;

    // Determine which channel:
        int channel = tableView.tag;  // tag=channel, set at init time


    // Prepare to update cell:
        // DOCUMENTATION:  Table View Programming Guide for iOS > Adding subviews to a cell’s content view
        // Give each cell a cell identifier unique to each channel tableView and unique to each row, so that each gets a unique data structure:
        NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",channel,indexPath.row];

        //static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            // if nil: cell(chan, row) has not been created before.  <>nil: cell = data structure previously initialized
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
        }

    // Erase anything previously displayed in the cell, by drawing cell-size big, white label:
        font_size = 10.0;
        // Top, left corner of cell:
            y = 0;  
            x = 0;
        // Entire area of cell:
            h = CHANNEL_ROW_HEIGHT;      // height of cell
            w = channel_tableView_width;   // width of cell
        UILabel* index_label = [[UILabel alloc] initWithFrame: CGRectMake( x,y, w,h)];
        index_label.backgroundColor = [UIColor whiteColor];
        index_label.textAlignment = NSTextAlignmentLeft; // NSTextAlignmentCenter, NSTextAlignmentLeft    NSTextAlignmentRight
        index_label.textColor=[UIColor darkGrayColor];
        index_label.numberOfLines=1;
        index_label.font = [UIFont systemFontOfSize: font_size];
        index_label.text = [NSString stringWithFormat:   @"" ];     
        //index_label.text = [NSString stringWithFormat:   @" *LAST %d *", ++last_ind];     //  normally ""
        [cell.contentView addSubview:index_label ];
        [index_label release];   <<<<<<<<<<<<<<<<<<<   CAUSES ARC COMPILE ERROR
         return cell; 

}

4

2 回答 2

2

每次使单元格出列时,您都将index_label子视图添加到每个单元格。您最终将多次添加标签并增加内存使用量;但是,这不是内存泄漏,而是您的逻辑问题。当单元被破坏时,内存将被回收。

解决方案很简单:UILabel在您的cell XIBPrototype Cell代码cell == nil部分中创建您的。这些选项中的哪一个是合适的,取决于您是如何编写应用程序的;我个人使用带有原型单元的故事板。

于 2013-09-01T01:46:33.733 回答
2

您每次都在为每个单元分配和添加 index_label。所以每次都在增加内存。您可以在 (cell == nil) 块中创建 index_label 并为 index_label 分配一些标签以每次访问标签以更新 index_label 的属性。

解决方案

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    int   row = indexPath.row;
    float font_size;
    UITextView*  PWR_RX_cover_box;
    int x,y,w,h;

    // Determine which channel:
    int channel = tableView.tag;  // tag=channel, set at init time


    // Prepare to update cell:
    // DOCUMENTATION:  Table View Programming Guide for iOS > Adding subviews to a cell’s content view
    // Give each cell a cell identifier unique to each channel tableView and unique to each row, so that each gets a unique data structure:
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d_%d",channel,indexPath.row];

    //static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // if nil: cell(chan, row) has not been created before.  <>nil: cell = data structure previously initialized
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];

        UILabel* index_label = [[UILabel alloc] initWithFrame: CGRectZero];
        index_label.backgroundColor = [UIColor whiteColor];
        index_label.textAlignment = NSTextAlignmentLeft; // NSTextAlignmentCenter, NSTextAlignmentLeft    NSTextAlignmentRight
        index_label.textColor=[UIColor darkGrayColor];
        index_label.numberOfLines=1;
        index_label.font = [UIFont systemFontOfSize: font_size];
           [cell.contentView addSubview:index_label ];
        index_label.tag=TAG_VALUE;
    }

    // Erase anything previously displayed in the cell, by drawing cell-size big, white label:
    font_size = 10.0;
    // Top, left corner of cell:
    y = 0;
    x = 0;
    // Entire area of cell:
    h = CHANNEL_ROW_HEIGHT;      // height of cell
    w = channel_tableView_width;   // width of cell

    UILabel* index_label=[cell.contentView viewWithTag:TAG_VALUE];
    index_label.text = [NSString stringWithFormat:   @"" ];
    index_label.frame=CGRectMake( x,y, w,h);
    return cell;
}
于 2013-09-01T14:48:29.660 回答